Sign in to follow this  
Maevik

Polymorphism Question

Recommended Posts

Maevik    122
If ClassA inherits from ClassB And I have a vector<ClassB*> how do I pass that to a function that takes a parameter ( vector<ClassA*> ) ?

Share this post


Link to post
Share on other sites
Atrix256    539
I'm kind of surprised you can't do that... hrm.

As a hacky work around you could do a vector of "void *" and cast to void * when storing, and cast back to A* when recalling.

There has to be a better way than that though.

Oh wait duhh...

why do you have a vector<ClassB*>?

Just make that a vector<ClassA*> and store ClassB*'s inside of it.

Share this post


Link to post
Share on other sites
jyk    2094
Quote:
I'm kind of surprised you can't do that... hrm.
It shouldn't be too surprising. vector<A*> and vector<B*> are unrelated types, even if A and B are related, just like jpetrie said (see the link he provided for more info).
Quote:
As a hacky work around you could do a vector of "void *" and cast to void * when storing, and cast back to A* when recalling.

There has to be a better way than that though.
I'm sure there's a better solution than using void pointers :) However, we don't really know what the OP is trying to do, so it's a little hard to offer specific suggestions.

@The OP: Can you tell us a little more about the context?

Share this post


Link to post
Share on other sites
taz0010    277
Usually the vector holds pointers to the base classes, and you call the derived class methods virtually. Alternatively you can loop over the vector and cast the base class pointers into the derived type. You normally use dynamic_cast for this, as it performs runtime checks to make sure you're casting to the right object.

Share this post


Link to post
Share on other sites
DevFred    840
You could write a function template that takes a vector of any pointer type.

#include <algorithm>
#include <functional>

template<class T>
void function(const std::vector<T*>& vec)
{
std::for_each(vec.begin(), vec.end(), std::mem_fun(&T::draw));
}

Share this post


Link to post
Share on other sites
iMalc    2466
Quote:
Original post by Atrix256
Just make that a vector<ClassA*> and store ClassB*'s inside of it.
No can do...
It seems that you've fallen into the trap of thinking that ClassB derives from ClassA, when in fact he specified that ClassA derives from ClassB. I.e. ClassB is the base class. Personally I think he should have given his example the way around that you were thinking.


My main point would be that to even consider doing what the OP desires (if it were directly possible), one would have to first guarantee that every item in the vector of ClassB does happen to actually be an instance of ClassA (or something derived form ClassA). If you are able to make such a determination beforehand, then why were all those ClassA objects not simply in a vector of ClassA*'s to begin with?

Templating this function you wish to call as per DevFred's example could certainly be one good aproach, but maybe the best answer would be more obvious with a little more real-world context, rather than just a theoretical example.

Share this post


Link to post
Share on other sites
return0    508
Quote:
Original post by iMalc
Quote:
Original post by Atrix256
Just make that a vector<ClassA*> and store ClassB*'s inside of it.
No can do...
It seems that you've fallen into the trap of thinking that ClassB derives from ClassA, when in fact he specified that ClassA derives from ClassB. I.e. ClassB is the base class. Personally I think he should have given his example the way around that you were thinking..



While the OP said B <- A, I'll bet they meant A <- B.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this