inheritance - problem with identyfing object

J

jacek

I have a:
class A
and a:
class B : public A

then I have a:
vector<A*> v;
which contains objects of both A and B classes.

when doing
for (int i=0; i<v.size(); i++)
{
}
I want to identify if each v is an object of class A or B.
sizeof(A) and sizeof(B) give the same unfortunately.
any hints?

jacek
 
J

Jeff Schwab

jacek said:
I have a:
class A
and a:
class B : public A

then I have a:
vector<A*> v;
which contains objects of both A and B classes.

when doing
for (int i=0; i<v.size(); i++)
{
}
I want to identify if each v is an object of class A or B.
sizeof(A) and sizeof(B) give the same unfortunately.
any hints?

jacek



My preferred method would be to give class A a public virtual
"is_subclass" method returning false, and override the method in class B
to return true. An alternative is to use dynamic_cast; see your
favorite C++ reference for details.
 
V

Victor Bazarov

Jeff Schwab said:
jacek said:
I have a:
class A
and a:
class B : public A

then I have a:
vector<A*> v;
which contains objects of both A and B classes.

when doing
for (int i=0; i<v.size(); i++)
{
}
I want to identify if each v is an object of class A or B.
sizeof(A) and sizeof(B) give the same unfortunately.
any hints?

jacek



My preferred method would be to give class A a public virtual
"is_subclass" method returning false, and override the method in class B
to return true. An alternative is to use dynamic_cast; see your
favorite C++ reference for details.


I'd say that anybody's preferred method should be to re-examine the need
to identify the actual type. If you store them in one vector, you must
have had a reason. That reason ought to have told you that you need to
have polymorphic behaviour instead of finding out what type the pointers
point to.

Victor
 
J

Jeff Schwab

Victor said:
Jeff Schwab said:
jacek said:
I have a:
class A
and a:
class B : public A

then I have a:
vector<A*> v;
which contains objects of both A and B classes.

when doing
for (int i=0; i<v.size(); i++)
{
}
I want to identify if each v is an object of class A or B.
sizeof(A) and sizeof(B) give the same unfortunately.
any hints?

jacek



My preferred method would be to give class A a public virtual
"is_subclass" method returning false, and override the method in class B
to return true. An alternative is to use dynamic_cast; see your
favorite C++ reference for details.



I'd say that anybody's preferred method should be to re-examine the need
to identify the actual type. If you store them in one vector, you must
have had a reason. That reason ought to have told you that you need to
have polymorphic behaviour instead of finding out what type the pointers
point to.


Good point, Victor. Of course, I agree.
 
J

jacek

Jeff and Victor,
thanks for all help!
you're absolutely right about polymorphism, I'm gonna refactor the source
code when I have more time, hopefully tomorrow

jacek
 
B

bartek

I have a:
class A
and a:
class B : public A

then I have a:
vector<A*> v;
which contains objects of both A and B classes.

when doing
for (int i=0; i<v.size(); i++)
{
}
I want to identify if each v is an object of class A or B.
sizeof(A) and sizeof(B) give the same unfortunately.
any hints?


Visitor pattern, maybe?

'zdrowka!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,169
Messages
2,570,919
Members
47,458
Latest member
Chris#

Latest Threads

Top