call constructor from base class

M

mwebel

Hi ikind of remember this being easy but cant find it on google:
i have a base class and a derived class.
The base class has two constructors normal and int overloaded one.
thing is i want the derived class to call the constructors for the base
class using also the same kind of overloading. like this:

class BASE{
public:
BASE();
BASE(int x);
}

class DERIVED : BASE{
//normal constructor
DERIVED(){
//init BASE with normal constructor
};
//overloaded constructor
DERIVED(int){
//init BASE with overloaded constructor
};
}

int main(){

//this should call the overloaded constructor of BASE!!!
DERIVED dummy(3);
return 1;
}

it would be my death if the wrong base constructor would be called or
even called twice...
thanks for any help!
 
M

mlimber

Hi ikind of remember this being easy but cant find it on google:
i have a base class and a derived class.
The base class has two constructors normal and int overloaded one.
thing is i want the derived class to call the constructors for the base
class using also the same kind of overloading. like this:

Just put the proper base constructor call in your derived class'
initialization list. See below.
class BASE{

Don't use all caps for your names. Reserve that for macros only (and
use those only sparingly!).
public:
BASE();
BASE(int x);
}

class DERIVED : BASE{
//normal constructor
DERIVED()
: BASE() // Would be called by default anyway
{
//init BASE with normal constructor
};
//overloaded constructor
DERIVED(int i) : BASE( i )
{
//init BASE with overloaded constructor
};
}

int main(){

//this should call the overloaded constructor of BASE!!!
DERIVED dummy(3);

It will!!!
return 1;
}

it would be my death if the wrong base constructor would be called or
even called twice...
thanks for any help!

Cheers! --M
 

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

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,241
Members
46,831
Latest member
RusselWill

Latest Threads

Top