Calling parent constructor from child constructor

Discussion in 'C++' started by pantalaimon, Oct 9, 2004.

  1. pantalaimon

    pantalaimon Guest

    I'm trying to write a GUI for a game I'm making. Till now I've always
    done this: ChildClass(int x,int y) : ParentClass(x,y) whenever my
    compiler complains about "no default constructor found". But in one of
    my classes I need to do some calculations first and THEN call the
    parent's constructor. Here is an example:

    class ParentClass
    {
    public:
    Parentclass(int sum);
    };

    class ChildClass : public ParentClass
    {
    public:
    ChildClass(int x,int y)
    {
    int sum = x + y;
    ParentClass(sum);
    }

    Problem is I don't know how to do it (if it is even possible). The
    above code doesn't work. The compiler thinks I want to instantiate an
    instance of the ParentClass.
    Thanks in advance!
    pantalaimon, Oct 9, 2004
    #1
    1. Advertising

  2. * pantalaimon:
    > I'm trying to write a GUI for a game I'm making. Till now I've always
    > done this: ChildClass(int x,int y) : ParentClass(x,y) whenever my
    > compiler complains about "no default constructor found". But in one of
    > my classes I need to do some calculations first and THEN call the
    > parent's constructor. Here is an example:
    >
    > class ParentClass
    > {
    > public:
    > Parentclass(int sum);
    > };
    >
    > class ChildClass : public ParentClass
    > {
    > public:
    > ChildClass(int x,int y)
    > {
    > int sum = x + y;
    > ParentClass(sum);
    > }
    >
    > Problem is I don't know how to do it (if it is even possible). The
    > above code doesn't work. The compiler thinks I want to instantiate an
    > instance of the ParentClass.


    ChildClass( int x, int y ): ParentClass( x + y ) {}

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
    Alf P. Steinbach, Oct 9, 2004
    #2
    1. Advertising

  3. (pantalaimon) writes:

    > I'm trying to write a GUI for a game I'm making. Till now I've always
    > done this: ChildClass(int x,int y) : ParentClass(x,y) whenever my
    > compiler complains about "no default constructor found". But in one of
    > my classes I need to do some calculations first and THEN call the
    > parent's constructor. Here is an example:
    >
    > class ParentClass
    > {
    > public:
    > Parentclass(int sum);
    > };
    >
    > class ChildClass : public ParentClass
    > {
    > public:
    > ChildClass(int x,int y)
    > {
    > int sum = x + y;
    > ParentClass(sum);
    > }


    This doesn't work because the constructor of ParentClass is called
    automatically before processing the constructor of ChildClass You can not work
    around this, because the code is inserted by the compiler, but you can
    do the following:

    ChildClass(int x, int y) : ParentClass(x+y)
    {
    }

    By doing this the compiler shouldbe satisfied and should do the right
    thing.

    BTW You can make the scenario I described "visible" by trying this:

    #include <iostream>
    using namespace std;

    class Base
    {
    public:
    Base()
    {
    cout << "Base::Base() called" << endl;
    }
    ~Base()
    {
    cout << "Base::~Base() called()" << endl;
    }

    };

    class Derived : public Base
    {
    public:
    Derived()
    {
    cout << "Derived::Derived() called" << endl;
    }
    ~Derived()
    {
    cout << "Derived::~Derived() called" << endl;
    }
    };

    int main()
    {
    Derived object;
    return(0);
    }

    The output should be:

    Base::Base() called
    Derived::Derived() called
    Derived::~Dervied() called
    Base::~Base() called

    HTH && Kind regards,
    Nicolas
    --
    | Nicolas Pavlidis | Elvis Presly: |\ |__ |
    | Student of SE & KM | "Into the goto" | \|__| |
    | | ICQ #320057056 | |
    |-------------------University of Technology, Graz----------------|
    Nicolas Pavlidis, Oct 9, 2004
    #3
  4. "pantalaimon" <> wrote in message
    news:...
    > I'm trying to write a GUI for a game I'm making. Till now I've always
    > done this: ChildClass(int x,int y) : ParentClass(x,y) whenever my
    > compiler complains about "no default constructor found". But in one of
    > my classes I need to do some calculations first and THEN call the
    > parent's constructor. Here is an example:
    >
    > class ParentClass
    > {
    > public:
    > Parentclass(int sum);
    > };
    >
    > class ChildClass : public ParentClass
    > {
    > public:
    > ChildClass(int x,int y)
    > {
    > int sum = x + y;
    > ParentClass(sum);
    > }
    >
    > Problem is I don't know how to do it (if it is even possible). The
    > above code doesn't work. The compiler thinks I want to instantiate an
    > instance of the ParentClass.
    > Thanks in advance!


    If your calculation is as simple as x + y then do as Alf and Nicolas have
    already indicated. If your calculation is a bit more complex then a static
    function sometimes works well.

    class ParentClass
    {
    public:
    ParentClass(int sum);
    };

    class ChildClass : public ParentClass
    {
    public:
    ChildClass(int x,int y) : ParentClass(complex_calculation(x, y))
    {
    }
    private:
    static int complex_calculation(int x, int y)
    {
    ...
    }
    };

    john
    John Harrison, Oct 9, 2004
    #4
    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. Jeff Rodriguez
    Replies:
    23
    Views:
    1,083
    David Schwartz
    Dec 9, 2003
  2. Brandt Dusthimer
    Replies:
    4
    Views:
    303
    Jim Langston
    Jul 12, 2006
  3. Brandt Dusthimer
    Replies:
    2
    Views:
    284
    Howard
    Jul 12, 2006
  4. Noel Dolan
    Replies:
    0
    Views:
    215
    Noel Dolan
    Jul 18, 2004
  5. Bitswapper
    Replies:
    5
    Views:
    118
    Prasad, Ramit
    Aug 27, 2013
Loading...

Share This Page