By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,191 Members | 751 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,191 IT Pros & Developers. It's quick & easy.

STL list

P: n/a
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!
Jul 22 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Asapi wrote:
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.

Jul 22 '05 #2

P: n/a
Asapi wrote:
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.

Jul 22 '05 #3

P: n/a

Asapi wrote:
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.


But do not use std::auto_ptr. I'm sure the gurus already knew that.
Jul 22 '05 #4

P: n/a
Walter Kalata wrote:
Asapi wrote:
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.
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

Jul 22 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.