adum
asked on
easy: virtuals in STL vector
Hi, I want to have an STL vector of a class and its derived classes. The problem is, when I pop elements from the vector, then call their virtual functions, they get executed as a base class.
That probably sounds like goop, so let me illustrate:
class mom {
virtual void foo { cout << "mommy";
};
class son : public mom {
virtual void foo { cout << "sonny";
};
main() {
vector<mom> vec;
son s;
s.foo(); // outputs "sonny"
vec.push_front(s);
vec.front().foo() // outputs "mommy", but I want it to be "sonny"
So, my question is, is there a way to do what I want? Am I just missing something obvious? Or do I need a messier solution, like <vector *mom>, which works, but is ugly.
thanks,
adum
That probably sounds like goop, so let me illustrate:
class mom {
virtual void foo { cout << "mommy";
};
class son : public mom {
virtual void foo { cout << "sonny";
};
main() {
vector<mom> vec;
son s;
s.foo(); // outputs "sonny"
vec.push_front(s);
vec.front().foo() // outputs "mommy", but I want it to be "sonny"
So, my question is, is there a way to do what I want? Am I just missing something obvious? Or do I need a messier solution, like <vector *mom>, which works, but is ugly.
thanks,
adum
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Hmmm, where is my answer? gone.
Ok what I wanted to say.
Try:
Vector<&mom)
Now you are just copying the mom part of son and putting that one the vector
Ok what I wanted to say.
Try:
Vector<&mom)
Now you are just copying the mom part of son and putting that one the vector
One way around this would be to use pointers (as you did) but that is messy because the extra indirection syntax, also you may need to be doing some new's and deletes that you didn't before.
Another way is to use references, like vector<mom &>. That is similar to pointers, but you don't have the extra syntax. However, you would have to make sure that the objects stored are not destroyed during the time they are in the vector. That is possible for some cases, but impossible (at least really inconvenient) for most.
One last solution, is to use "smart pointers". (this is a little different than a standard smart pointer--somethimes its called a facet.) You can create smart pointers for the mom class that appear to act like a mom object--or a derived object, but dont really contain the object, they just have a pointer to the object. This class could be stored in the array. It would be equivalent to storing the pointer to the objects in the array, except it is much more convenient. You don't have the pointer syntax, you don't have the new and deletes.
Another way is to use references, like vector<mom &>. That is similar to pointers, but you don't have the extra syntax. However, you would have to make sure that the objects stored are not destroyed during the time they are in the vector. That is possible for some cases, but impossible (at least really inconvenient) for most.
One last solution, is to use "smart pointers". (this is a little different than a standard smart pointer--somethimes its called a facet.) You can create smart pointers for the mom class that appear to act like a mom object--or a derived object, but dont really contain the object, they just have a pointer to the object. This class could be stored in the array. It would be equivalent to storing the pointer to the objects in the array, except it is much more convenient. You don't have the pointer syntax, you don't have the new and deletes.
ASKER
Thanks for your excellent help, Nietod (and also Mirkwood). I think using facets is the way to go.
adum
adum
mom = son
(those are the types involved, not the variable names) This is legal if son is derived from mom, but it uses a technique "object slicing". Essentially it slices the extra parts of the son and stores only the parts that are also in a mom. In particular it creates a mom that contains all the mom data members that were in son. It also has all the virtual function that were associated with mom, not son.