473,854 Members | 1,562 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Storing different types in container

How can objects of different types be stored in a collection?
For example:

struct S1 { /*...*/ };
struct S2 { /*...*/ };
struct Sn { /*...*/ };

template<typena me 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)?

Jul 23 '05 #1
17 2611
ben

Can these objects be stored in the same collection class (for example in a

vector or map)?

No unless you try to use dangerous vector<void*>.

ben
Jul 23 '05 #2
ben wrote:
Can these objects be stored in the same collection class (for
example in a vector or map)?
No unless you try to use dangerous vector<void*>.


Or the alluring vector<boost::a ny> ;-)
ben


Jonathan
Jul 23 '05 #3
tuvok wrote:
How can objects of different types be stored in a collection?
For example:

struct S1 { /*...*/ };
struct S2 { /*...*/ };
struct Sn { /*...*/ };

template<typena me 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)?


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.

Regards,
Larry
Jul 23 '05 #4
"Larry I Smith" <la***********@ verizon.net> wrote
tuvok wrote:
How can objects of different types be stored in a collection?
For example:

struct S1 { /*...*/ };
struct S2 { /*...*/ };
struct Sn { /*...*/ };

template<typena me 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)?


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.


Something like the following?

template<typena me T> class X : public B { /* see above */ }

How would this be added to a std::vector?
Jul 23 '05 #5
Larry I Smith wrote:
tuvok wrote:
How can objects of different types be stored in a collection?
For example:

struct S1 { /*...*/ };
struct S2 { /*...*/ };
struct Sn { /*...*/ };

template<type name 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)?

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.

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
--
Artie Gold -- Austin, Texas
http://it-matters.blogspot.com (new post 12/5)
http://www.cafepress.com/goldsays
Jul 23 '05 #6
Artie Gold wrote:
Larry I Smith wrote:
tuvok wrote:
How can objects of different types be stored in a collection?
For example:

struct S1 { /*...*/ };
struct S2 { /*...*/ };
struct Sn { /*...*/ };

template<typena me 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)?

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.

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


Yes, you are correct. Base class pointers only...

Larry
Jul 23 '05 #7
"Larry I Smith" <la***********@ verizon.net> wrote in message
news:WHPqe.418$ 1q5.323@trnddc0 2...
Artie Gold wrote:
Larry I Smith wrote:
tuvok wrote:

How can objects of different types be stored in a collection?
For example:

struct S1 { /*...*/ };
struct S2 { /*...*/ };
struct Sn { /*...*/ };

template<typena me 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)?

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.

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


Yes, you are correct. Base class pointers only...


Write a class to encapsulate the base class and hide the management of
object lifetime using boost::shared_p tr. Objects (not pointers to objects)
of the base-encapsulating class can be stored in a std::vector like any
other object.
Jul 23 '05 #8
"tuvok" <52************ ***@t-online.de> wrote in message
news:d8******** *****@news.t-online.com...
"Jason Heyes" <ja********@opt usnet.com.au> wrote
"Larry I Smith" <la***********@ verizon.net> wrote in message
> Artie Gold wrote:
>> Larry I Smith wrote:
>>> tuvok wrote:
>>>
>>>> Can these objects be stored in the same collection class (for
>>>> example
>>>> in a vector or map)?
>>>>
>>> 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.
>>>
>> 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
>
> Yes, you are correct. Base class pointers only...
>


Write a class to encapsulate the base class and hide the management of
object lifetime using boost::shared_p tr. Objects (not pointers to
objects)
of the base-encapsulating class can be stored in a std::vector like any
other object.


Will then the right virtual functions be invoked?


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_p tr<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.
Jul 23 '05 #9
tuvok wrote:
How can objects of different types be stored in a collection?
For example:

struct S1 { /*...*/ };
struct S2 { /*...*/ };
struct Sn { /*...*/ };

template<typena me 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)?


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.

Jul 23 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
7205
by: alg | last post by:
Is it possible to put different types of objects in a single STL list? If not what STL template should be used which will contains various objects of different types? Thanks for your help!
4
2791
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 |bo|int | char |etc.
6
2587
by: Alfonso Morra | last post by:
I have written the following code, to test the concept of storing objects in a vector. I encounter two run time errors: 1). myClass gets destructed when pushed onto the vector 2). Prog throws a "SEGV" when run (presumably - attempt to delete deleted memory. Please take a look and see if you can notice any mistakes I'm making. Basically, I want to store classes of my objects in a vector. I also have three further questions:
13
2558
by: Dan Tsafrir | last post by:
is the following code standard? (cleanly compiles under g++-4.0.2): struct Asc { bool operator()(int a, int b) {return a < b;} }; struct Des { bool operator()(int a, int b) {return b > a;} }; int main() { int arr = {1, 2, 3}; set<int,Asc> asc(arr, arr+3); set<int,Des>::iterator beg = asc.begin(); // set<int,Des>::iterator end = asc.end(); // copy(beg, end, ostream_iterator<int>(cout,...
2
2475
by: Manish Naik | last post by:
Hi, Using ASP.Net, I want to store and retrive documents (Word, excel, etc) from SQL Server 2000 database. I have tried image type data field, but could not succed. Can any one help me please. Regards, Manish Naik
1
1699
by: glen stark | last post by:
Hi everyone. I would like to consider the following problem: I want to provide the ability to store and manipulate numeric container classes that provide 1) element wise access 2) size but maybe not with the same interface. For example MatrixType1 provides element access, and size1() and size2() for size access. MatrixType2 provide (i,j) element access. Now, these matrix classes might contain doubles, floats, integers,
5
2367
by: fff_afafaf | last post by:
Do you know is it possible to put different kinds of tuples to one container? E.g. to a vector? (The lengths of the tuples are different, and also the types in the tuples are different.. -Is it possible to make a pointer, which can point to all of these tuples?) #include <tr1/tuple>
11
2378
by: toton | last post by:
Hi, all of the containers in STL stores object itself (thus copy contsructability & assignability is needed), while NTL or boost ptr_container stores pointer to the object in the container (either exclusively owns, or just stores). Now, my question is for a general guideline when to use which one? What I understand, 1) polymorphic objects need ptr_container. 2) non copy constructable, non assignable objects need ptr_container.
5
1861
by: toton | last post by:
Hi, I have a deque of Point class,. Point class have two fields x, and y. Now I want another class Character should point to a portion of the deque, and allow iteration only on the portion. Thus a deque<Characterwill point on the deque<Pointover different ranges. Both are dynamic in the sense, new character, and hence new point gets added, while old character and old points get removed. My question is, what the Character will store as...
0
9901
marktang
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9751
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9512
agi2029
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7914
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7079
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5940
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4557
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 we have to send another system
2
4153
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3185
bsmnconsultancy
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.