Re: STL string inheritance

From: David Hilsee (davidhilseenews_at_yahoo.com)
Date: 10/03/04


Date: Sun, 3 Oct 2004 11:23:00 -0400


"Mike Tyka" <m.tyka@gnx.net> wrote in message news:I50E4K.GFG@bath.ac.uk...
<snip>
> Thanks a lot for all your answers, i think i'm slowly getting my head
round
> this stuff.. I kept the example generic for simplicity's sake.
> I want to create a data structure and a correcponding mask data structure,
> both of
> which have all of the properties of a string, and it would be useful to
have
> all it's functionality
> (like cutting, concatenating etc etc). At the same time both data
structures
> will want to have
> additional functionality, specialised to the type of data they're holding,
> but no more additional
> data - so the lack of virtual destructors etc shouldnt matter !?Hence the
> inherited approach seems
> sensible in this case ?
>
> Am i thinking along the right lines here ?

Even if the derived class contains no additional data, it's still undefined
behavior if you pass delete a std::string pointer when it is a pointer to an
instance of the derived class. Of course, you can avoid doing that in your
programs, and it's hard to accidentally do it simply because std::string has
no virtual member functions.

I tend to gripe about public inheritance of standard containers because it
is using inheritance to expose an implementation detail as a "nameless
public member". Many people choose to do that instead of having a public
member because inheritance is generally considered "better" than public
members. Unfortunately, in this case, it's roughly the same thing. It's a
general problem when inheritance is used for code reuse purposes (discussed
in the FAQ a bit in the Smalltalk/C++ comparison -- 30.4). However, for
most smaller programs, it doesn't really matter.

-- 
David Hilsee


Relevant Pages

  • Re: STL string inheritance
    ... >> interface that you want MyString to have. ... >> provide this functionality, then inheriting from the container might ... but it's about the same as public inheritance from the ... I want to create a data structure and a correcponding mask data structure, ...
    (comp.lang.cpp)
  • Re: Code reuse
    ... You asked specifically about 2 things: Inheritance and Interfaces. ... functionality, rather than functions existing outside of any class. ... re-use the base class and its functionality in each derived class. ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: Equvalent of "Shadows" in VB.Net?
    ... anything to do with my control, in a correct way, my control is for a ... One of the facts of life and inheritance. ... > member by defining a new member that can have a different access level, ... > protected override void OnBackColorChanged ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: C# Pattern for shadowing member variables?
    ... Name hiding through inheritance occurs when classes or structs redeclare ... A method introduced in a class or struct hides all non-method base class ... A declaration of a new member hides an inherited member only within the ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: super() woes (n00b)
    ... print("I am a member of class P") ... That is more or less the text from the "Quick Python Book". ... Super is meant to solve some issues about ... multi inheritance, especially diamond diagram inheritance. ...
    (comp.lang.python)