Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

easy: virtuals in STL vector

Posted on 1999-01-22
5
279 Views
Last Modified: 2013-12-14
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
0
Comment
Question by:adum
  • 3
5 Comments
 
LVL 22

Accepted Solution

by:
nietod earned 100 total points
ID: 1184859
The only easy way is to use <vector *mom>.

continues.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1184860
The problem is that a vector<mom> really only stores mom's   if you try to store a son (or any class derived from mom) in it at some point it executes a line like

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.
0
 
LVL 13

Expert Comment

by:Mirkwood
ID: 1184861
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
0
 
LVL 22

Expert Comment

by:nietod
ID: 1184862
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.  
0
 

Author Comment

by:adum
ID: 1184863
Thanks for your excellent help, Nietod (and also Mirkwood). I think using facets is the way to go.
adum
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Programmer's Notepad is, one of the best free text editing tools available, simply because the developers appear to have second-guessed every weird problem or issue a programmer is likely to run into. One of these problems is selecting and deleti…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question