Sign in to follow this  

A basic C++ question about classes

This topic is 3580 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi. I've got a question, I'm sure it's pretty basic, but I'd like to know what the options are. I have a (STL) list of class instances, all of them of different classes, but all derived from a particular base class. There's a virtual function in the base class implemented by the derived classes. I want to just pick an object in the list and call this function, but I'm under the impression that unless I re-cast it to the right (derived) type (which I don't know!), the base virtual function will be called instead of the derived one? Is that right or not, and if it is, what are the alternatives? (BTW, when copying one of these lists or manipulating them, the original derived classes will be maintained right? It won't do something stupid like copying one of the instances as if it was of the base class, even though the list is defined as a list of instances of the base class?)

Share this post


Link to post
Share on other sites
You cannot store derived classes in a container for a base class. If you attempt to do Base base = Derived(), object slicing will occur. If you want to use inheritance and polymorphism, you will need to use references or pointers. In other words, your list cannot be std::list<Base> but rather std::list<Base*>.

When using pointers, the usual problems occur. You have to take care that everything is deallocated (which is not that hard), even when exceptions occur (which requires a lot of attention). The good thing is that someone else has already thought about this one and we have boost.ptr_container and boost.shared_ptr to help with this. So, rather than using std::list<Base*>, you could use boost::ptr_list<Base> or std::list<boost::shared_ptr<Base> > to get rid of the pointer issues. The former is probably better unless you need shared ownership (which might not be a good idea).

Hope this helps,
-Riku

Share this post


Link to post
Share on other sites
I have no idea where you got that "impression". The whole point of 'virtual' is to avoid having to know the type ahead of time.

It's easy to make it work, but you can't put derived objects into a "slot" reserved for a base object - they don't "fit". For this reason, we usually add a layer of indirection - but beware memory management issues! The Boost library can help you out here: consider for example boost::ptr_list, or a std::list of boost::shared_ptr to whatever.

Share this post


Link to post
Share on other sites
I'm sure it was a mistake in my code that gave me that impression. A long coding session and suddenly shoehorning a feature like this left me with messy code and really confused! Thank you very much to y'all, I'll probably use the boost library then :)

Share this post


Link to post
Share on other sites

This topic is 3580 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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