don't want to retype the base class ctor

H

Hicham Mouline

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,
 
P

Pascal J. Bourguignon

Hicham Mouline said:
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
*/
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top