Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

container to store multiple types

Status
Not open for further replies.

lakshmivaragan

Programmer
Sep 30, 2003
25
0
0
IN
Is there a way I could store multiple objects of different types in a container, say, a vector or map or an array?

e.g
template <class T>
class Holder
{
public:
T * m_ptr;
Holder(T * ptr)
{
m_ptr = ptr;
}
};

class Cls1
{
xxx...
};

class Cls2
{
xxx...
};

class Cls3
{
xxx...
};

int main()
{
Cls1 ptr1 = new Cls1;
Cls2 ptr2 = new Cls2;
Cls3 ptr3 = new Cls3;

Holder<Cls1> hobj1(ptr1);
Holder<Cls2> hobj2(ptr2);
Holder<Cls3> hobj3(ptr3);

//How to store the holder objects hobj1, hobj2, hobj3
// in a STL container or array?
}
 
I understand there is boost::any
Apart from this, is there any workaround possible with C++ standard libraries itself?
 
Derive them from the same empty base class
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top