Polymorphism Question

Started by
6 comments, last by return0 14 years ago
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*> ) ?
Advertisement
You don't. vector<ClassA*> and vector<ClassB*> are distinct types, even though ClassA and ClassB may be related. See here.
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.
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?
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.
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));}
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.
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms
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.

This topic is closed to new replies.

Advertisement