Topic: Inheritance of constructor/desctructor


Author: J B<jb.1234abcd@googlemail.com>
Date: Wed, 19 Dec 2012 14:20:12 -0800 (PST)
Raw View
Hi,

I have a question about definition of inheritance (perhaps as stated in C++
ISO docs somewhere).

Somebody claimed that when one class is derived from another, then the base
class' constructors and desctructors are inherited by the derived class.

Let's consider the simplest case:
class Base {};
class Derived: public Base {};

I tend to disagree and this is my argument:

The definition of inheritance (biologic, scientific,  etc) states that it is
an acquisition of traits genetically transmitted from parent to offspring.
Well, in that case those traits in a parent are not modifiable by an offspring
in any way, only some of parent's "blueprint" is utilized in creation
of offspring's
"blueprint".

So, a derived class does not inherit constructors and desctructors of a base
class.

The purpose of inheritance is to create a condition for polymorphism
(an ability to substitute objects thru dynamic binding).

Constructors/destructors of a base class should not and can not be directly
exposed to a derived class to be possibly manipulated, e.g. hidden, overriden,
or overloaded.
This would lead to dynamic changes to the "blueprint" (which a base class is)
according to which instances (objects) are created.

The derived class may only automatically call the constructor and destructor
of a base class when it executes its own constructor or destructor, per
compiler setup, and not by user actions (programmatically, or any other way).

What is your opinion or that of official docs, if any ?

jb


--
[ comp.std.c++ is moderated.  To submit articles, try posting with your ]
[ newsreader.  If that fails, use mailto:std-cpp-submit@vandevoorde.com ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]




Author: James Kuyper<jameskuyper@verizon.net>
Date: Thu, 20 Dec 2012 11:49:30 -0800 (PST)
Raw View
On 12/19/2012 05:20 PM, J B wrote:
>  Hi,
>
>  I have a question about definition of inheritance (perhaps as stated in C++
>  ISO docs somewhere).
>
>  Somebody claimed that when one class is derived from another, then the base
>  class' constructors and desctructors are inherited by the derived class.
>
>  Let's consider the simplest case:
>  class Base {};
>  class Derived: public Base {};
>
>  I tend to disagree and this is my argument:
>
>  The definition of inheritance (biologic, scientific,  etc) states that it is
>  an acquisition of traits genetically transmitted from parent to offspring.
>  Well, in that case those traits in a parent are not modifiable by an offspring
>  in any way, only some of parent's "blueprint" is utilized in creation
>  of offspring's
>  "blueprint".
>
>  So, a derived class does not inherit constructors and desctructors of a base
>  class.

The C++ standard defines what it means by "inheritance"; any conflicting
concept of inheritance derived from the meanings that same word has in
biological or legal contexts should be ignored.

The C++ standard has an entire section (12.9) devoted to "Inheriting
constructors", so I think that constructors can be inherited - but what
that section is talking about is probably quite different from what
you're talking about. It's not something that automatically occurs just
because one class is derived from another. It specifically requires the
use of a using-declaration, such as

     struct D1 : B1 {
         using B1::B1;
     };


--
[ comp.std.c++ is moderated.  To submit articles, try posting with your ]
[ newsreader.  If that fails, use mailto:std-cpp-submit@vandevoorde.com ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]