Re: C++ design question

From: Universe (universe_at_tAkEcovad.OuT.net)
Date: 09/28/04


Date: Tue, 28 Sep 2004 14:12:10 -0400

Universe <universe@tAkEcovad.OuT.net> wrote:

> "Simon Elliott" <Simon at ctsn.co.uk> wrote:
>
> > I've been advised by the guys over on comp.lang.c++ to post this here
> > to see if anyone can recommend a design improvement. In the following
> > code example, the base class depends on an object in a derived class:
> > fooBase has a reference to an object in fooDerived. It's been pointed
> > out that this is potentially unsafe if bar_base_ref_ is accessed in the
> > fooBase constructor.
> >
> > Can anyone improve on this aspect of the design? Here's the code:
> >
> > class barBase
> > {
> > protected:
> > int i1_;
> > public:
> > barBase(int i1):i1_(i1){}
> > virtual ~barBase(void){};
> > void SetI1(int i1){i1_ = i1;}
> > };
> >
> > class barDerived:public barBase
> > {
> > protected:
> > int i1_;
> > int i2_;
> > public:
> > 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.

> > virtual ~barDerived(void){};
> > };
> >
> > class fooBase
> > {
> > private:
> > barBase &bar_base_ref_;
> > private:
> > fooBase(const fooBase &C);
> > fooBase& operator=(const fooBase &C);
> > public:
> > fooBase(barBase &bar_base_ref):bar_base_ref_(bar_base_ref){}
> > virtual ~fooBase(void){};
> > void DoStuffWithBarBase(void)
> > {
> > bar_base_ref_.SetI1(13);
> > }
> > };
> >
> > class fooDerived:public fooBase
> > {
> > private:
> > barDerived my_bar_derived_;
> > public:
> > fooDerived(int i1, int i2):fooBase(my_bar_derived_),

Similarly:

> > fooDerived(int i1, int i2):fooBase(my_bar_derived_),
> > my_bar_derived_(42,43){}

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.

Further there is a "fawlty" attempt to initialize a base class instance
with a 'my_bar_derived' instance where 'my_bar_derived' is only
initialized *after* it is assigned to the base class instance.

> > virtual ~fooDerived(void){};
> > };
> >
> > int main(int argc, char **argv)
> > {
> > fooDerived my_foo_derived(42,1);
> > my_foo_derived.DoStuffWithBarBase();
> > return 0;
> > }
> >

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.

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.

Elliott

-- 
dip refers to *abstract interfaces* a form of class
abstraction.  The opposite form is a concrete
implementation class abstraction.


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. ... > class instance part if that is 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)