reg constructors/copy constructors inheritance

Discussion in 'C++' started by srp113, Feb 17, 2009.

  1. srp113

    srp113 Guest

    Hello,
    I have a base class A, and class D derived from A. I declare a
    constructor(ctr) for B() but in definition I dont explicitly call
    constructor for A. Compiler however seems to take care of this and
    when I construct an object of D, it calls A's ctr first followed by
    D's ctr. I tried to see if same holds true for copy constructors: I
    defined copy constructor for A and D, and D's copy ctr didnt include
    any calls for calling A's copy ctr. In this case though, what happens
    is A's default ctr (NOT copy ctr) is called and then B's copy ctr. Why
    this discrepancy? In general if you define a constructor (default/
    copy) in derived class is it better to include an explicit call to
    corr. base classes constructor?
    Thanks,
    Sunil
     
    srp113, Feb 17, 2009
    #1
    1. Advertising

  2. * srp113:
    > Hello,
    > I have a base class A, and class D derived from A. I declare a
    > constructor(ctr) for B() but in definition I dont explicitly call
    > constructor for A. Compiler however seems to take care of this and
    > when I construct an object of D, it calls A's ctr first followed by
    > D's ctr. I tried to see if same holds true for copy constructors: I
    > defined copy constructor for A and D, and D's copy ctr didnt include
    > any calls for calling A's copy ctr. In this case though, what happens
    > is A's default ctr (NOT copy ctr) is called and then B's copy ctr. Why
    > this discrepancy?


    For the question of "why" you'd better ask in [comp.std.c++].

    But regarding what the rules are, not why the are: the automatically generated
    calls do not depend on the arguments to the constructor in question.

    Copy constructors are treated specially in some ways, namely (1) that a copy
    constructor can be automatically generated, (2) that a templated constructor is
    never a copy constructor (i.e. you can still get an automatically generated one)
    and (3) that copy constructor calls can be elided in certain cases even when the
    copy constructor has some side effect, i.e. that the compiler is free to treat a
    copy constructor as if it really just copied and nothing else.

    But copy constructors are not treated specially with regard to initialization of
    bases and members.

    So, if you define a copy constructor without using an initializer list to call
    base class and member copy constructors, those will default-initialized -- and
    if they cannot be default-initialized the compiler will protest very loudly.


    > In general if you define a constructor (default/
    > copy) in derived class is it better to include an explicit call to
    > corr. base classes constructor?


    For a copy constructor: yes, assuming that by "call" you mean to call it via an
    initializer list, like

    struct D: A
    {
    D( D const& other ): A( other ) { ... }
    };


    Cheers & hth.,

    - Alf


    PS: I think you meant to write either just "D" or just "B".
     
    Alf P. Steinbach, Feb 17, 2009
    #2
    1. Advertising

  3. srp113

    James Kanze Guest

    On Feb 17, 1:13 am, srp113 <> wrote:

    > I have a base class A, and class D derived from A. I declare
    > a constructor(ctr) for B() but in definition I dont explicitly
    > call constructor for A. Compiler however seems to take care of
    > this and when I construct an object of D, it calls A's ctr
    > first followed by D's ctr. I tried to see if same holds true
    > for copy constructors: I defined copy constructor for A and D,
    > and D's copy ctr didnt include any calls for calling A's copy
    > ctr. In this case though, what happens is A's default ctr (NOT
    > copy ctr) is called and then B's copy ctr. Why this
    > discrepancy?


    There's no discrepancy. If a base class has a constructor (of
    any type), then all derived class constructors will call a
    constructor of the base class. If you don't specify arguments
    for it in the initialization list, it will call the default
    constructor for the base class; the one without arguments. A
    copy constructor is no exception to this rule. (Obviously, in
    the case of a copy constructor, this is rarely what we want, but
    it's still up to you to define something else.)

    (Copy constructors are a bit special in other ways: if you don't
    declare one, the compiler will, and if the compiler declared
    copy constructor is used, the compiler will also define it. And
    the compiler defined copy constructor copies all of the base
    classes and members---using their copy constructors.)

    > In general if you define a constructor (default/ copy) in
    > derived class is it better to include an explicit call to
    > corr. base classes constructor?


    It depends. If I want the base class default constructed, I'll
    usually just skip it, unless the base class is a POD. But
    anytime you want anything other than default construction, you
    have to explicitly mention the class in the initialization list.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, Feb 17, 2009
    #3
  4. srp113

    Guest

    On Feb 17, 1:03 pm, James Kanze <> wrote:
    > On Feb 17, 1:13 am, srp113 <> wrote:
    >
    > > I have a base class A, and class D  derived from A. I declare
    > > a constructor(ctr) for B() but in definition I dont explicitly
    > > call constructor for A. Compiler however seems to take care of
    > > this and when I construct an object of D, it calls A's ctr
    > > first followed by D's ctr. I tried to see if same holds true
    > > for copy constructors: I defined copy constructor for A and D,
    > > and D's copy ctr didnt include any calls for calling A's copy
    > > ctr. In this case though, what happens is A's default ctr (NOT
    > > copy ctr) is called and then B's copy ctr. Why this
    > > discrepancy?

    >
    > There's no discrepancy.  If a base class has a constructor (of
    > any type), then all derived class constructors will call a
    > constructor of the base class.  If you don't specify arguments
    > for it in the initialization list, it will call the default
    > constructor for the base class; the one without arguments.  A
    > copy constructor is no exception to this rule.  (Obviously, in
    > the case of a copy constructor, this is rarely what we want, but
    > it's still up to you to define something else.)
    >
    > (Copy constructors are a bit special in other ways: if you don't
    > declare one, the compiler will, and if the compiler declared
    > copy constructor is used, the compiler will also define it.  And
    > the compiler defined copy constructor copies all of the base
    > classes and members---using their copy constructors.)
    >
    > > In general if you define a constructor (default/ copy) in
    > > derived class is it better to include an explicit call to
    > > corr. base classes constructor?

    >
    > It depends.  If I want the base class default constructed, I'll
    > usually just skip it, unless the base class is a POD.  But
    > anytime you want anything other than default construction, you
    > have to explicitly mention the class in the initialization list.
    >
    > --
    > James Kanze (GABI Software)             email:
    > Conseils en informatique orientée objet/
    >                    Beratung in objektorientierter Datenverarbeitung
    > 9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34


    There is no discrepancy between compile defined constructor and
    compile defined copy constructor,
    which calls its corresponding base counterpart.

    But there is no discrepancy between user defined constructor and user
    defined copy constructor(both without initialization list),which calls
    base class cotr. (Would it make more sense to make user defined copy
    cotr without initialization list to call its base class copy cotr in
    the standard?)

    Gob00st
     
    , Feb 17, 2009
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Russ Ford
    Replies:
    1
    Views:
    5,337
    Ron Natalie
    Nov 20, 2003
  2. Alex
    Replies:
    2
    Views:
    1,293
  3. Jeremy Smith
    Replies:
    2
    Views:
    622
    Jeremy Smith
    Aug 3, 2006
  4. Replies:
    3
    Views:
    309
    Jens Theisen
    Oct 8, 2006
  5. Jess
    Replies:
    5
    Views:
    644
    Ron Natalie
    Jun 7, 2007
Loading...

Share This Page