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

Started by
2 comments, last by Nibbles 21 years, 2 months ago
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."
------------------------------------------------------------
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]
___________________________________________________________Where to find the intensity (Updated Dec 28, 2004)Member of UBAAG (Unban aftermath Association of Gamedev)
Derive your classes from your baseclass and store pointer to the objects. You''ll have to cast them back when using them (I think).

[My Lousy Page | Kings Of Chaos | Vampires | email.me]
[My Lousy Page | Kings Of Chaos | Vampires | [email=lordlethis@hotmail.com]email.me[/email]]
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.

This topic is closed to new replies.

Advertisement