473,396 Members | 1,927 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Different types of object in a single list?

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
6 7169
"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
"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

"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
"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
"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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Nils Petter Vaskinn | last post by:
Why is it that if I have an overloaded function like: void foo(int i); void foo(char *c); I can't do: void bar() { foo( condition ? 5 : "NULL"); }
7
by: Office Drone | last post by:
I'm a bit confused about memory usage, and for some reason I wasn't able to find a single point-of-call to get the amount of memory available. If we take, for instance, the Windows platform: ...
3
by: Eric | last post by:
I have a string representation of an object. I create an object of that type through reflection. I would like to create a List<> of those objects. I obviously can't do List<myObject.GetType()>...
6
by: Anders Würtz | last post by:
i have an assignment to iterate through a collection containing different types of numeric values (float, double, int, byte, short etc.) and to add 1 to all of them. I tried with array and...
2
by: mast2as | last post by:
Hi there, for a long time I've been trying to think of way of saving different data of different types using one single class (well 2 in reality, a class for the data, and 1 class for a list of...
8
by: Jay | last post by:
I'm trying to store a sequence of operations and values of different types into a single array. It's a sequence of command word bytes, and a sequence of one or more values (as determined by the...
15
by: Juha Nieminen | last post by:
I'm sure this is not a new idea, but I have never heard about it before. I'm wondering if this could work: Assume that you have a common base class and a bunch of classes derived from it, and...
11
by: pompair | last post by:
Hi, Does anyone know how to use anonymous types in a list? I mean, if you define in your code three anonymous types: new {FirstName = "Donald", LastName = "Duck"}; new {FirstName =...
5
by: girays | last post by:
I want to make a good design about combining information. Suppose you have different data types, and each may have semantically same data. I want to merge these different data types into a single...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.