"Jenny" <je***********@yahoo.com.au> wrote in message
news:69**************************@posting.google.c om...
I have a class foo which will construct some objects in my code. some
of the objects store int values into the data deque, while others
store float values to the deque.
.... After the objects finish storing the data into deques. I need another
deque or something store the objects. I was trying to use
std::deque<foo> foo_list; to store the ojbects. Because class foo is a
template class, I can only construct it as std::deque<foo<int> >
foo_list; or std::deque<foo<float> > foo_list;
However I need a deque to store both foo<float> and foo<int>. Is there
any other way I can use to store the foo objects in a list?
Previous posters have suggested:
- storing a textual representation into std::string-s
- a container of polymorphic elements (storing a ptr to a base class).
Another option would be to use a C-style union (usually in conjunction
with a typeId field), but let's not insist on this.
However, the problem you are facing is common enough, and encapsulated
solutions already exist: classes that can store a value of one of
several types. Some vendors (MS) call this a Variant, other call
them *discriminated unions* (because they are like C-style unions,
but provide information about the stored type).
http://www.google.com/search?q=discr...+union+C%2B%2B
The boost library provides such a class, called "boost::any".
You may want to give it a try:
http://www.boost.org/doc/html/any.html
In particular, take a look at the example:
http://www.boost.org/doc/html/ch02s02.html
Cheers,
Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Brainbench MVP for C++ <>
http://www.brainbench.com