Calling parent constructor from child constructor

P

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.
Thanks in advance!
 
A

Alf P. Steinbach

* 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 ) {}
 
N

Nicolas Pavlidis

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
 
J

John Harrison

pantalaimon said:
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
 

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,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top