Container for different types of variables

M

m@j3R ;

Hello

I need to use sth. like container for different types of variables.
Is sth like this in c++ STL?

----|----------|--|----|------|etc.
var1| var2 |v3|var4| var5 |etc.
int | float |bo|int | char |etc.


m@j3R ;)
 
J

Jonathan Turkanis

Mathew Hendry said:
No, but a collection of boost::any's might do the job. http://www.boost.org

It's possible, but you have to keep track somehow of the actual types of the
objects in the collection, or you can't get them back.

It really depends on what operations the OP needs to be able to perform on the
elements of the collection.

Jonathan
 
S

Siemel Naran

m@j3R ; said:
I need to use sth. like container for different types of variables.
Is sth like this in c++ STL?

----|----------|--|----|------|etc.
var1| var2 |v3|var4| var5 |etc.
int | float |bo|int | char |etc.

In C++, make a base abstract class Var. Now make classes Int, Float, etc
derived from it. Your container will be a std::deque<Var*>, or to make it
more easily memory safe std::deque<boost::shared_ptr<Var>>. Feel free to
add whatever pure virtual functions you want to class Var. You could make
your hierarchy more elaborate too, such as creating a class Float8 derived
from Float, Float16, etc.

You can use boost::any, as suggested in the other post. Looking at the
code, it seems the same as the above idea, except you don't have to create
class Var, Int, Float etc. So it should suffice in most circumstances.

Another thing is that one can think of std::eek:stringstream as a container of
heterogenous types.

std::eek:stringstream container;
container << intVal << ' ' << floatVal << ' ' << charVal << ' ' << boolVal;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top