Jump to content
  • Advertisement
Sign in to follow this  
leggyguy

Abstract types in a vector? C++ question

This topic is 4520 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

I haven't used vectors in C++, but have in Java a long time ago. Now, in java I was able to use asbtract classes within a vector, but when I tried to do so in VC++ 8, I got errors. Is it the case, then, that c++ stl vectors only supports classes which can be instantiated? I have InventoryItem, an abstract class, with 3 subclasses (Clothing, Misc and FoodDrink). I could create an array of InventoryItem objects, but if I want to use Vectors, I need to create 3 vectors, one for each possible item. Is this the right idea? Or am I muddled up? Thanks for any advice, I appreciate all help.

Share this post


Link to post
Share on other sites
Advertisement
Java only supports reference types (except for the primitive types), so essentially everything is a (garbage-collected) pointer. C++ support value or reference semantics. A std::vector< X > is a vector of X objects stored by value. If you add a Y object which is a subclass of X to the vector then only the X base object part will be placed in the std::vector. This is known as the slicing problem. If X is an abstract class then this operation will try to place an actual instance of a class which is not supposed to be possible to instantiate into the std::vector, hence it is disallowed.

To get reference semantics equivalent to Java you need to use a std::vector< X * > (or a std::vector< boost::shared_ptr< X > >, a boost::ptr_vector< X > or something equivalent if you don't want to do manual memory management - and you don't).

Σnigma

Share this post


Link to post
Share on other sites
Firstly, no you couldn't create an array of InventoryItem (you would end up with slicing issues if you put subclasses in there). What you could do is create an array of InventoryItem pointers. You can also create a vector of InventroyItem pointers.

Note, you may want to use smart pointers but you cannot use the stl auto_ptr, you could however look to boost if you do need smart pointers.

Share this post


Link to post
Share on other sites
The STL is a class of generics that hold specific types.

Now, you can store polymorphic values within the STL.

In Java, everything is "by pointer" by default, and the language handles it for you.

In C++, you can have actual "instances" of classes -- like you can have instances of integers in Java -- and you can use them. However, for the most part "instances" of classes cannot be polymorphic -- their type must be defined at compile time.

Both boost::any, boost::variant are useful if you want to have the possibility of multiple types stored in one variable. This is not the sa

However, if you want polymorphism, you have to deal with pointers.

Pointers can lead to memory leaks if used improperly (Java has an automatic garbage collector that cleans up the memory leaks). Learning how to deal with the resource management of pointers is tricky.

If you don't have circular reference loops, simple smart pointer classes (from boost -- such as boost::shared_ptr) do a decent job of helping with memory leaks. If you have a vector of
boost::shared_ptr<base_class>
and your base_class was properly virtual, then you'd get nice polymorphic behaviour.

Because of the default reference semantics and garbage collection, Java acts differently than C++ in many ways.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!