By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,375 Members | 1,111 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,375 IT Pros & Developers. It's quick & easy.

Different types of object in a single list?

P: n/a
alg
Is it possible to put different types of objects in a single STL list? If
not what STL template should be used which will contains various objects of
different types?

Thanks for your help!
Jul 19 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
"alg" <al******@yahoo.com> wrote...
Is it possible to put different types of objects in a single STL list? If
not what STL template should be used which will contains various objects of different types?


Please search the archives for "heterogeneous container".
The main question you should ask yourself is "why do I want
to have them in one container?"

Victor
Jul 19 '05 #2

P: n/a
"alg" <al******@yahoo.com> wrote in message
news:gI*********************@bgtnsc04-news.ops.worldnet.att.net...
Is it possible to put different types of objects in a single STL list? If
not what STL template should be used which will contains various objects of different types?

You can make the container store void *s.



--
Ioannis

* Programming pages: http://www.noicys.freeurl.com
* Alternative URL 1: http://run.to/noicys
* Alternative URL 2: http://www.noicys.cjb.net

Jul 19 '05 #3

P: n/a

"alg" <al******@yahoo.com> wrote in message
news:gI*********************@bgtnsc04-news.ops.worldnet.att.net...
Is it possible to put different types of objects in a single STL list? If
not what STL template should be used which will contains various objects of different types?

Thanks for your help!

This question does get asked often enough, and I always ask myself why? Why
do you need to store objects of different types and how do you plan to work
with them?

Suppose we do have a magical container that can store objects of different
types.
************************************************** *
A a;
B b;
C c;

MagicContainer list;
list.insert(a);
list.insert(b);
list.insert(c);
************************************************** *
all fair so far. Now how do we get stuff out of the list?

A *a = list.front(); ?
How do we know the element is of type A? Maybe its of type B, or C?
You see the issue?

So now you can either create the list as a (void*) list and magically know
what to cast the elements back to.
You could create some kind of container structure that includes the type.

struct
{
int myType;
void *data
}

You could create a union with an explicit type

struct
{
int myType;
union
{
A a;
B b;
C c;
}
}

You could do it the C++ way and create a common base class, make the list of
that type. Make all common operations virtual and away you go, its a
beautiful thing. If you want to get messy a bit and mix C/C++, include a
myType variable as part of the base class, and have each derived class set
that variable to its own thing. Then if you really need to, you know what
to cast it to using dynamic_cast.

Yamin
Jul 19 '05 #4

P: n/a
"Sergey Tursanov" <_g***@hippo.ru> writes:
"Yamin" <ab*****@sdfdasfsd.com> wrote in message
You could create a union with an explicit type

struct
{
int myType;
union
{
A a;
B b;
C c;
}
}


It seems to me that you cannot place classes with constructors in union.
Just because compiler don't know which constructor to call when you
initialize union.


You're absolutely correct, and the standard explicitly says that
classes with non-trivial constructors are not allowed as union
members. Quoting from section 9.5:

An object of a class with a non-trivial constructor (12.1), a
non-trivial copy constructor (12.8), a non-trivial destructor
(12.4), or a non-trivial copy assignment operator (13.5.3, 12.8)
cannot be a member of a union, nor can an array of such objects.

--
Raoul Gough
"Let there be one measure for wine throughout our kingdom, and one
measure for ale, and one measure for corn" - Magna Carta
Jul 19 '05 #5

P: n/a
"Victor Bazarov" <v.********@attAbi.com> wrote in message news:<EV0Ra.73078$Ph3.6922@sccrnsc04>...
"alg" <al******@yahoo.com> wrote...
Is it possible to put different types of objects in a single STL list? If
not what STL template should be used which will contains various objects

of
different types?


Please search the archives for "heterogeneous container".
The main question you should ask yourself is "why do I want
to have them in one container?"


Probably because the OP has learned to code in "another language"
where there are things *called* objects that are really variants.

It is possible to code up something that does much the same job
as a variant, and even make it fit properly in a standard container.
I'm not real sure if it's ever the best choice. Maybe if you were
writing something like a postscript interpreter where you needed
to have a stack that held all different kinds of objects from
characters, to procedures, to fonts, to dictionaries. Or maybe not.
Socks
Jul 19 '05 #6

P: n/a
"alg" <al******@yahoo.com> wrote in message
news:gI*********************@bgtnsc04-news.ops.worldnet.att.net...
Is it possible to put different types of objects in a single STL list? If
not what STL template should be used which will contains various objects of different types?

Thanks for your help!


The only time I needed to do this was when writing a console for a game, and
even then I was only dealing with POD's, which I stored in a union. But you
need to make sure you know you know for sure that you are getting/setting
the correct union members (by using some sort of flag) otherwise you will
see fireworks (seen plenty of those when debugging the console!)
S. Armondi
Jul 19 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.