call constructor from base class

Discussion in 'C++' started by mwebel@freenet.de, May 12, 2006.

  1. Guest

    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, 2006
    #1
    1. Advertising

  2. mlimber Guest

    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
     
    mlimber, May 12, 2006
    #2
    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. mrstephengross
    Replies:
    5
    Views:
    623
    Larry I Smith
    May 18, 2005
  2. Taran
    Replies:
    6
    Views:
    406
    Default User
    Apr 20, 2006
  3. ali
    Replies:
    4
    Views:
    629
    David Harmon
    Mar 5, 2007
  4. Rahul
    Replies:
    16
    Views:
    1,249
    terminator
    Nov 7, 2007
  5. Replies:
    2
    Views:
    748
Loading...

Share This Page