don't want to retype the base class ctor

Discussion in 'C++' started by Hicham Mouline, Jan 30, 2009.

  1. Hello,
    If I have a base class B with 1 explicit ctor with some signature,
    and I have D1. .... D10 that have no specific constructor.

    Is there a way not to have to _write_ the derived ctors that _just_ forwards
    to the base ctor ?

    regards,
     
    Hicham Mouline, Jan 30, 2009
    #1
    1. Advertising

  2. "Hicham Mouline" <> writes:

    > Hello,
    > If I have a base class B with 1 explicit ctor with some signature,
    > and I have D1. .... D10 that have no specific constructor.
    >
    > Is there a way not to have to _write_ the derived ctors that _just_ forwards
    > to the base ctor ?


    I would do:

    #include <iostream>
    using namespace std;

    class B {
    public:
    int val;
    B(int specific):val(specific){};
    };

    class D : public B {
    public:
    D():B(42){}
    };


    class D1 : public D {
    };

    class D2 : public D {
    };



    int main(){
    D1 d1;
    D2* d2=new D2();
    cout<<d1.val<<" "<<d2->val<<endl;
    return(0);
    }

    /*
    -*- mode: compilation; default-directory: "~/src/tests-c++/" -*-
    Compilation started at Fri Jan 30 15:41:24

    SRC="/home/pjb/src/tests-c++/ctor.c++" ; EXE="ctor" ; g++ -g3 -ggdb3 -o ${EXE} ${SRC} && ./${EXE} && echo status = $?
    42 42
    status = 0

    Compilation finished at Fri Jan 30 15:41:25
    */

    --
    __Pascal Bourguignon__
     
    Pascal J. Bourguignon, Jan 30, 2009
    #2
    1. Advertising

  3. Hicham Mouline wrote:
    > Hello,
    > If I have a base class B with 1 explicit ctor with some signature,
    > and I have D1. .... D10 that have no specific constructor.
    >
    > Is there a way not to have to _write_ the derived ctors that _just_ forwards
    > to the base ctor ?


    Not in the current C++ standard, but the next standard will have a
    syntax to do exactly that.

    http://en.wikipedia.org/wiki/C++0x#Object_construction_improvement
     
    Juha Nieminen, Jan 31, 2009
    #3
    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. Apricot
    Replies:
    4
    Views:
    559
    velthuijsen
    Apr 16, 2004
  2. Nataraj M
    Replies:
    4
    Views:
    344
    Frank Chang
    Jul 29, 2005
  3. NVH
    Replies:
    8
    Views:
    520
    mlimber
    Jul 6, 2006
  4. Grizlyk
    Replies:
    8
    Views:
    511
    Grizlyk
    Nov 29, 2006
  5. , India

    copy ctor vs default ctor

    , India, Aug 15, 2007, in forum: C++
    Replies:
    2
    Views:
    432
    =?ISO-8859-1?Q?Erik_Wikstr=F6m?=
    Aug 15, 2007
Loading...

Share This Page