Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   SGI STL slist Design (http://www.velocityreviews.com/forums/t745104-sgi-stl-slist-design.html)

MikeP 03-14-2011 06:48 PM

SGI STL slist Design
 
SGI STL's slist derives from a base class which has a private constructor
and destructor, no doubt to enforce that the base class is not useable by
itself. slist then uses private inheritance from the base class. Would
declaring the constructor and destructor of the base class protected and
deriving slist from the base using public inheritance accomplish the same
thing? And why bother giving the other member functions (erase_after) of
the base class other access control when the constructor and destructor
are already private?



Victor Bazarov 03-14-2011 07:15 PM

Re: SGI STL slist Design
 
On 3/14/2011 2:48 PM, MikeP wrote:
> SGI STL's slist derives from a base class which has a private constructor
> and destructor, no doubt to enforce that the base class is not useable by
> itself.


That sounds odd. If the class cannot be constructed except by itself or
a friend, then 'slist' has to be declared a friend of that class, no?
Otherwise, slist will not be able to construct the subobject of that
base class.

> slist then uses private inheritance from the base class. Would
> declaring the constructor and destructor of the base class protected and
> deriving slist from the base using public inheritance accomplish the same
> thing?


Of course not. Private inheritance prevents the implicit conversion.
Public inheritance provides implicit conversions. Exactly the opposite
if you think about it.

> And why bother giving the other member functions (erase_after) of
> the base class other access control when the constructor and destructor
> are already private?


Not sure what you're talking about here. Are we supposed to know what
"SGI STL's slist" is? If it's not a standard class/template, perhaps
you could provide some code snippets to illustrate what you mean.

V
--
I do not respond to top-posted replies, please don't ask

MikeP 03-14-2011 07:51 PM

Re: SGI STL slist Design
 
Victor Bazarov wrote:
> On 3/14/2011 2:48 PM, MikeP wrote:
>> SGI STL's slist derives from a base class which has a private
>> constructor and destructor, no doubt to enforce that the base class
>> is not useable by itself.

>
> That sounds odd. If the class cannot be constructed except by itself
> or a friend, then 'slist' has to be declared a friend of that class,
> no?


No, because it uses private inheritance which gives access to the base
class privates including member functions?

> Otherwise, slist will not be able to construct the subobject of
> that base class.
>
>> slist then uses private inheritance from the base class. Would
>> declaring the constructor and destructor of the base class protected
>> and deriving slist from the base using public inheritance accomplish
>> the same thing?

>
> Of course not. Private inheritance prevents the implicit conversion.


Yes. Private inheritance means "is implemented in terms of". The
alternative is to do composition.

> Public inheritance provides implicit conversions.


Yes. Public inheritance means "is a". erase_after is given protected
access, so the derived slist IS-A slist_base, or so it implied. I'm sure
there are subtleties in the design, but that's what I am trying to figure
out.

Why worry about implicit conversions to a base class who's type will
never ever be used in a scenario where such a conversion could occur?
slist_base will never show up in code outside of the STL library itself.
It's internal.

> Exactly the
> opposite if you think about it.
>
>> And why bother giving the other member functions (erase_after) of
>> the base class other access control when the constructor and
>> destructor are already private?

>
> Not sure what you're talking about here. Are we supposed to know what
> "SGI STL's slist" is? If it's not a standard class/template, perhaps
> you could provide some code snippets to illustrate what you mean.


Oh, I thought it didn't need more explanation and that it was clear. If
not though, one could perusal stl_slist.h in the SGI STL code:
http://www.sgi.com/tech/stl/. I did not excerpt code from there because
it would have meant posting a bunch of extraneous information.




Marcel Müller 03-14-2011 07:58 PM

Re: SGI STL slist Design
 
MikeP wrote:
> SGI STL's slist derives from a base class which has a private constructor
> and destructor,


nope.

Constructor and destructor of _Slist_base are public. Note that
_Slist_base is a /struct/ rather than a class.


Marcel

MikeP 03-14-2011 08:36 PM

Re: SGI STL slist Design
 
Marcel Müller wrote:
> MikeP wrote:
>> SGI STL's slist derives from a base class which has a private
>> constructor and destructor,

>
> nope.
>
> Constructor and destructor of _Slist_base are public. Note that
> _Slist_base is a /struct/ rather than a class.
>


Yes!! I missed that. Ah the subtleties of C++.



MikeP 03-14-2011 08:42 PM

Re: SGI STL slist Design
 
MikeP wrote:
> SGI STL's slist derives from a base class which has a private
> constructor and destructor,


This is not correct: the base is a struct, so the constructor and
destructor are public.

> no doubt to enforce that the base class
> is not useable by itself. slist then uses private inheritance from
> the base class.


Or so I reasoned when thinking the base was a class.




All times are GMT. The time now is 10:15 AM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57