Hi,
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.
template <class TYPE>
class foo
{
protected:
std::string name;
std::deque<TYPE> data;
};
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? 7 4065
Jenny wrote: Hi,
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.
template <class TYPE> class foo { protected: std::string name; std::deque<TYPE> data; };
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?
You need polymorphic behaviour, thus
* Derive foo from a common base classs
* store pointers in the deque instead of objects.
class fooBase
{
protected:
std::string name;
};
template <class TYPE>
class foo : public fooBase
{
protected:
std::deque<TYPE> data;
};
std::deque<fooBase*> foo_list;
Attention: Since you are storing pointers, you are now responsible
for deleting the foo objects. The deque won't do it any longer.
You can get around this by using a smart pointer you can get
eg. at www.boost.org
--
Karl Heinz Buchegger kb******@gascad.at
"Jenny" <je***********@yahoo.com.au> wrote in message
news:69**************************@posting.google.c om... Hi,
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.
template <class TYPE> class foo { protected: std::string name; std::deque<TYPE> data; };
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?
Polymorphism (as recommended by Karl) is the usual way to do this. But in
this case you have another choice, store your ints and floats as strings.
class foo
{
protected:
std::string name;
std::deque<std::string> data;
};
It's easy enough to convert from strings to ints and floats and back. Easy
enough to tell if a string is an int or a float. Just a suggestion and a bit
of a hack, but maybe it would be a good idea.
john
"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
"John Harrison" <jo*************@hotmail.com> wrote in message news:2s*************@uni-berlin.de... "Jenny" <je***********@yahoo.com.au> wrote in message news:69**************************@posting.google.c om... Hi,
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.
template <class TYPE> class foo { protected: std::string name; std::deque<TYPE> data; };
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?
Polymorphism (as recommended by Karl) is the usual way to do this. But in this case you have another choice, store your ints and floats as strings.
class foo { protected: std::string name; std::deque<std::string> data; };
It's easy enough to convert from strings to ints and floats and back. Easy enough to tell if a string is an int or a float. Just a suggestion and a bit of a hack, but maybe it would be a good idea.
john
It would be a good idea not only for ints and floats, but for other types if are using stream_cast.
stream_cast() was discussed by Dietmar Kuehl at
* http://groups.google.com/groups?selm...nnrp1.deja.com
To be sure that stream_cast conversion is bijective, one could use functions which detect stream_cast bijectivity
* http://groups.google.com/groups?th=8bb967e46c5e3a87
* http://groups.google.com/groups?thre...0uni-berlin.de
--
Alex Vinokur
email: alex DOT vinokur AT gmail DOT com http://mathforum.org/library/view/10978.html http://sourceforge.net/users/alexvn
"John Harrison" <jo*************@hotmail.com> writes: "Jenny" <je***********@yahoo.com.au> wrote in message news:69**************************@posting.google.c om... Hi,
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.
template <class TYPE> class foo { protected: std::string name; std::deque<TYPE> data; };
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?
Polymorphism (as recommended by Karl) is the usual way to do this. But in this case you have another choice, store your ints and floats as strings.
class foo { protected: std::string name; std::deque<std::string> data; };
It's easy enough to convert from strings to ints and floats and back. Easy enough to tell if a string is an int or a float. Just a suggestion and a bit of a hack, but maybe it would be a good idea.
Alternatively you can use boost::any:
class foo
{
protected:
std::string name;
std::deque<boost::any> data;
};
And then you check the type of the object stored in runtime
data[i].type() == typeid();
int x = boost::any_cast<int>(data[i]);
Or you can use boost::variant:
class foo
{
protected:
std::string name;
std::deque< boost::variant<int, float> > data;
};
And then:
if (int* x = boost::get<int>(data[i]))
do_smth_on_int(*x);
else if (float* x = boost::get<float>(data[i]))
do_smth_on_float(*x);
--
WBR, Max Vasin
JID: ma******@jabber.ru
ICQ: 276438891
> Alternatively you can use boost::any: Or you can use boost::variant:
Doh! I never heared of these two. And wow, boost::any looks really
interesting...
-Gernot
On 4 Oct 2004 05:13:47 -0700, je***********@yahoo.com.au (Jenny)
wrote: Hi,
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.
template <class TYPE> class foo { protected: std::string name; std::deque<TYPE> data; };
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?
boost::variant lets you do this:
typedef boost::variant<foo<int>, foo<float> > types;
std::deque<types> d;
d.push_back(aFooFloat);
d.push_back(aFooInt);
Then apply_visitor is the best way to get at the values, but you may
also use get:
foo<float>& foo = get<foo<float> >(d[0]);
See http://www.boost.org/doc/html/variant.html.
In your case, a better approach might be to give foo a non-templated
base class, and use a deque<shared_ptr<foo_base> >.
Tom This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: m |
last post by:
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...
|
by: R.Z. |
last post by:
i was wondering whether it pays off in terms of memory use to maintain lots
of empty deques (it would be convenient for my algorithms but memory use is
more important). and does the size of a deque...
|
by: ALiX |
last post by:
Hi all,
In my code I use different vectors of the same size to hold some data.
At some point I need to iterate through all vectors at the same time.
Now, the question is what type should the...
|
by: Russell Warren |
last post by:
Does anyone have an easier/faster/better way of popping from the middle
of a deque than this?
class mydeque(deque):
def popmiddle(self, pos):
self.rotate(-pos)
ret = self.popleft()...
|
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...
| |
by: Chris |
last post by:
I'm not sure if this has been done before, but I couldn't easily find
any prior work on Google, so here I present a simple decorator for
documenting and verifying the type of function arguments....
|
by: newbie |
last post by:
Let's see two different usage of an STL container. I see (2) more
often when reading code over (1), dose that give any benefit or it's
purely a coding preference?
Also, please see the (3), I see...
|
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...
|
by: Edward Jensen |
last post by:
Hi,
I have the following static arrays of different size in a class:
in header:
static double w2, x2;
static double w3, x3;
static double w4, x4;
in GaussLegendre.cpp:
|
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,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
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: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| |