C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

deriving from STL containers

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
sb
Guest





PostPosted: Wed Feb 25, 2004 3:13 pm    Post subject: deriving from STL containers Reply with quote



We had a discussion about this in c.l.c++ ("implicit constructor"
thread). I thought I'd ask here, since people seem to be, on average,
more intelligent here than over in the unmoderated group for some
reason.

When is it OK to derive from STL containers and when is it not OK? The
reasons should be specific to STL containers, and not concrete types
with all non-virtual methods in general.

For example, what is wrong with

struct named_vector : public vector<int> {
string name;
};

if I'm never going to let vector<int>* "own" named_vector ?

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
tom_usenet
Guest





PostPosted: Thu Feb 26, 2004 12:10 am    Post subject: Re: deriving from STL containers Reply with quote



On 25 Feb 2004 10:13:11 -0500, [email]spam_bait101 (AT) yahoo (DOT) com[/email] (sb) wrote:

Quote:
We had a discussion about this in c.l.c++ ("implicit constructor"
thread). I thought I'd ask here, since people seem to be, on average,
more intelligent here than over in the unmoderated group for some
reason.

When is it OK to derive from STL containers and when is it not OK? The
reasons should be specific to STL containers, and not concrete types
with all non-virtual methods in general.

For example, what is wrong with

struct named_vector : public vector<int> {
string name;
};

if I'm never going to let vector<int>* "own" named_vector ?

There are two situations where you might want something like that:

1. A utility class used all over the place. In this case, there is a
danger than someone somewhere sometime will get confused and delete it
through a vector pointer. In this case it might be more appropriate to
use private inheritence and re-expose the interface with using
declarations.

2. A business logic class. In this case, it's unlikely that you want
the whole vector interface, and you probably need to intercept some of
the vector logic in any case due to other invariants, etc.

However, if you are encapsulating the named_vector (e.g. its a private
nested class), then I don't see much harm. Turning a vector<int> into
a named_vector can be a bit of a pain to do efficiently. e.g.

named_vector v;
v.name = "Tom";
v.swap(normalvector);

There is has been discussion in the standards committee about making
delegation and therefore the ideal implementation (via aggregation) a
bit easier to write by adding some language support for argument
forwarding, etc.

Tom
--
C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Dhruv Matani
Guest





PostPosted: Thu Feb 26, 2004 12:20 am    Post subject: Re: deriving from STL containers Reply with quote



On Wed, 25 Feb 2004 10:13:11 -0500, sb wrote:

[I haven't read the discussion as yet, but I'm sure that this particular
point will have been mentioned by someone or the other].

Quote:
For example, what is wrong with

struct named_vector : public vector<int> {
string name;
};

if I'm never going to let vector<int>* "own" named_vector ?

Consider this:
std::vector<int>* piv = new named_vector;
delete piv;


--
Regards,
-Dhruv.

Proud to be a Vegetarian.
http://www.vegetarianstarterkit.com/
http://www.vegkids.com/vegkids/index.html


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.