B
ben
vector or map)?Can these objects be stored in the same collection class (for example in a
No unless you try to use dangerous vector<void*>.
ben
vector or map)?Can these objects be stored in the same collection class (for example in a
ben said:No unless you try to use dangerous vector<void*>.
Or the alluring vector said:
tuvok said:How can objects of different types be stored in a collection?
For example:
struct S1 { /*...*/ };
struct S2 { /*...*/ };
struct Sn { /*...*/ };
template<typename T> class X
{
public:
T data;
X() {}
//...
};
//...
X<S1> x1;
X<S2> x2;
X<Sn> xn;
Can these objects be stored in the same collection class (for example in a vector or map)?
Erm...Larry said:IF they all derive from the same base class (e.g. circle, rectangle,
triangle all derive from shape), then you can store them as
objects of the base class (e.g. as 'shape'). Otherwise - no.
Larry I Smith said:IF they all derive from the same base class (e.g. circle, rectangle,
triangle all derive from shape), then you can store them as
objects of the base class (e.g. as 'shape'). Otherwise - no.
Artie said:Erm...
You can store *pointers* to them (but not the objects themselves) as
*pointers to the base class* in this case; of course in that case, you'd
have to manage the objects' lifetimes manually.
HTH,
--ag
Larry I Smith said:Yes, you are correct. Base class pointers only...
Jason Heyes said:Write a class to encapsulate the base class and hide the management of
object lifetime using boost::shared_ptr. Objects (not pointers to objects)
of the base-encapsulating class can be stored in a std::vector like any
other object.
tuvok said:Will then the right virtual functions be invoked?
tuvok said:How can objects of different types be stored in a collection?
For example:
struct S1 { /*...*/ };
struct S2 { /*...*/ };
struct Sn { /*...*/ };
template<typename T> class X
{
public:
T data;
X() {}
//...
};
//...
X<S1> x1;
X<S2> x2;
X<Sn> xn;
Can these objects be stored in the same collection class (for example in a vector or map)?
Axter said:You can create a Heterogeneous Container if all the types have a
commone method or data type.
A Heterogeneous Container is a container of different types that have
NO common base type.
This method requires a wrapper class, that gives access to the common
method and/or data.
See following links for example code:
http://code.axter.com/HeterogeneousContainer1.cpp
http://code.axter.com/HeterogeneousContainer2.cpp
http://code.axter.com/HeterogeneousContainer3.cpp
Some compilers may have problems with the last example.
tuvok said:How can objects of different types be stored in a collection?
Phil said:
ben said:In addtion, you need all the classes to be polymorphic to be able to
cast from void* to the appropriate type.
Jason said:Absolutely. For example, say Animal is your base and Animal has a virtual
function called make_noise. Here is the encapsulating class (called a
handle) for the Animal base class:
class AnimalHandle
{
boost::shared_ptr<Animal> animal_ptr;
public:
AnimalHandle() : animal_ptr(new Animal()) { }
void make_noise() const { animal_ptr->make_noise(); }
};
You can store AnimalHandle objects in std::vector. You don't need to store
pointers to AnimalHandle.
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.