• Advertisement

Archived

This topic is now archived and is closed to further replies.

if it's possible, how can i have a list that holds dif...

This topic is 5489 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, using C++, how can i have a list that holds different kinds of data? for example: class Cclass1 { ... } class Cclass2 { ... } ... vector list; ... now list could either be from Cclass1, or Cclass2. does that make sense? if not, please hold you laughter Thanks, Scott
------------------------------------------------------------
Email
Website

"If you try and don''t succeed, destroy all evidence that you tried."
------------------------------------------------------------

Share this post


Link to post
Share on other sites
Advertisement
use a union. I've never done it before, but I think you do something like this:


    
class class1
{};
class class2
{};
union myUnion
{
class1, class2
};

myUnion u;
class1 obj1;
class2 obj2;

u = obj1;
u = obj2;


The type of u will change when you make it equal to an object of a different type.

Proceeding on a brutal rampage is the obvious choice.

[edited by - amish1234 on February 8, 2003 2:50:34 PM]

Share this post


Link to post
Share on other sites
First of all, you can''t use unions if your classes have any constructors - ie, the classes have to be POD.
If the two types are not related already, then you can use boost::any (www.boost.org, I''m too lazy to write a href)...
However, you will have to know what each type inserted is. (in order to get the actual instance as a Cclass1 or Cclass2).
If they are related, then hold a boost::shared_ptr of the base class type.

-----------------------------
Gamedev for learning.
libGDN for putting it all together.

Share this post


Link to post
Share on other sites

  • Advertisement