By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,194 Members | 884 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,194 IT Pros & Developers. It's quick & easy.

inheritance - problem with identyfing object

P: n/a
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[i] is an object of class A or B.
sizeof(A) and sizeof(B) give the same unfortunately.
any hints?

jacek
Jul 22 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
jacek wrote:
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[i] 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.
Jul 22 '05 #2

P: n/a
"Jeff Schwab" <je******@comcast.net> wrote...
jacek wrote:
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[i] 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
Jul 22 '05 #3

P: n/a
Victor Bazarov wrote:
"Jeff Schwab" <je******@comcast.net> wrote...
jacek wrote:
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[i] 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.
Jul 22 '05 #4

P: n/a
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

Jul 22 '05 #5

P: n/a
"jacek" <bo***********************@o2.pl> wrote in news:c7m9rr$c5a$1
@sunflower.man.poznan.pl:
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[i] is an object of class A or B.
sizeof(A) and sizeof(B) give the same unfortunately.
any hints?


Visitor pattern, maybe?

'zdrowka!
--
:: bartekd [at] o2 [dot] pl

Jul 22 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.