Re: C++ design question

From: Simon Elliott (Simon)
Date: 09/29/04


Date: 29 Sep 2004 08:47:19 GMT

On 28/09/2004, Universe wrote:
> > > barDerived(int i1, int i2):i2_(i2),barBase(i1){}
>
> > ^ Please explain the above line. ^
> >
> > Elliott
>
> The reason I ask is because as I understand doing:
>
> > > barDerived(int i1, int i2):i2_(i2),barBase(i1){}
>
> does not initialize the specific base class instance created as a part
> of creating an instance of this derived class if that was the intent.

Are you sure? My reading of the C++ standard (12.6.2) and tracing the
flow of execution in the debugger seem to indicate otherwise.

> 1) The derived classes ctor expressions do not initialize their base
> class instance part if that is the intent. They initialize some base
> class instance, but by the time one gets to initializing a base class
> instance the base class instance that is part of the derived class is
> *already* constructed and incorporated into the larger derived class
> instance.

As above, are you sure about this?

> 2) It is definitely not recommended in C++ to attempt to initialize
> the base class instance part of a derived class instance from a
> derived class instance in the same statement that creates the derived
> class instance.
>
> Off the top I would create a structure 'struct' external to both the
> base and derived classes that holds the derived classes members that I
> want their base classes to interact with.

> See "Fragile Base Class' in Struvstrup's FAQ at his web site for more
> particulars on the external structure solution.

I can't find a reference to this anywhere on
http://www.research.att.com/~bs/. Do you have any other pointers to
this issue?

-- 
Simon Elliott    http://www.ctsn.co.uk


Relevant Pages

  • Re: C++ design question
    ... does not initialize the specific base class instance created as a part ... of creating an instance of this derived class if that was the intent. ...
    (comp.object)
  • Re: C++ design question
    ... >> does not initialize the specific base class instance created as a part ... >> of creating an instance of this derived class if that was the intent. ... >> class instance part if that is the intent. ...
    (comp.object)
  • Re: C# inheritance broken?
    ... the knowledge that the derived class may still require some ... takes an object of the base class as it's argument. ... derived class instance" is completely unsafe and therefore runs ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: C# inheritance broken?
    ... the knowledge that the derived class may still require some initialization ... since this is occurring in a constructor in place of the ... derived class instance" is completely unsafe and therefore runs ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Ruby Syntax: initialize versus init
    ... There is a difference in wrapping to interface to C ... and return the class instance. ... Ruby is specially designed to be friendly to use and read. ... 'initialize' is within that friendly spirit. ...
    (comp.lang.ruby)