470,815 Members | 1,284 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,815 developers. It's quick & easy.

call constructor from base class

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!

May 12 '06 #1
1 1715
mw****@freenet.de wrote:
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

May 12 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

23 posts views Thread by Fabian Müller | last post: by
8 posts views Thread by trying_to_learn | last post: by
4 posts views Thread by Greg | last post: by
5 posts views Thread by Frederick Gotham | last post: by
11 posts views Thread by dolphin | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.