STL list

A

Asapi

class base
{
};

class d1 : public base
{
};

class d2 : public base
{
};

Then a bunch of objects instantiated from either d1 or d2.

Is it okay to put those objects into the same STL "list" container?

Thanks!
 
E

E. Robert Tisdale

Asapi said:
class base {
};

class d1 : public base {
};

class d2 : public base {
};

Then a bunch of objects instantiated from either d1 or d2.

Is it okay to put those objects into the same STL "list" container?

No, but you can place base* pointers to them in a list<base*> container.
 
R

red floyd

Asapi said:
class base
{
};

class d1 : public base
{
};

class d2 : public base
{
};

Then a bunch of objects instantiated from either d1 or d2.

Is it okay to put those objects into the same STL "list" container?

Thanks!

No, it's not ok to put those objects in the same list, because you'll get the d1's and the d2's
(and their children) truncated to base's.

HOWEVER.... It's OK to store POINTERS to those objects into the same std::list. DISCLAIMER for the
gurus: Use your favorite shared_ptr implementation (probably boost) rather than raw pointers.
 
W

Walter Kalata

No, it's not ok to put those objects in the same list, because you'll get the d1's and the d2's
(and their children) truncated to base's.

HOWEVER.... It's OK to store POINTERS to those objects into the same std::list. DISCLAIMER for the
gurus: Use your favorite shared_ptr implementation (probably boost) rather than raw pointers.

But do not use std::auto_ptr. I'm sure the gurus already knew that.
 
R

red floyd

Walter said:
the d1's and the d2's


std::list. DISCLAIMER for the


rather than raw pointers.


But do not use std::auto_ptr. I'm sure the gurus already knew that.

Whoops, you're absolutely right, I forgot to put that disclaimer in as well. However, technically, auto_ptr is not shared,
since it hands off ownership on any copy/assignment. But let's not get into that nitpicky a debate here --
there are plenty of other nitpicky debates around :p
 

Ask a Question

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.

Ask a Question

Members online

Forum statistics

Threads
474,156
Messages
2,570,878
Members
47,405
Latest member
DavidCex

Latest Threads

Top