Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Calling parent constructor from child constructor

Reply
Thread Tools

Calling parent constructor from child constructor

 
 
pantalaimon
Guest
Posts: n/a
 
      10-09-2004
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!
 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      10-09-2004
* 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?
 
Reply With Quote
 
 
 
 
Nicolas Pavlidis
Guest
Posts: n/a
 
      10-09-2004
http://www.velocityreviews.com/forums/(E-Mail Removed) (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:erived() called" << endl;
}
~Derived()
{
cout << "Derived::~Derived() called" << endl;
}
};

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

The output should be:

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

HTH && Kind regards,
Nicolas
--
| Nicolas Pavlidis | Elvis Presly: |\ |__ |
| Student of SE & KM | "Into the goto" | \|__| |
| (E-Mail Removed) | ICQ #320057056 | |
|-------------------University of Technology, Graz----------------|
 
Reply With Quote
 
John Harrison
Guest
Posts: n/a
 
      10-09-2004

"pantalaimon" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ...
> 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
If a class Child inherits from Parent, how to implementChild.some_method if Parent.some_method() returns Parent instance ? metal Python 8 10-30-2009 10:31 AM
A constructor calling another constructor (default constructor)? Generic Usenet Account C++ 10 11-28-2007 04:12 AM
Map of Parent Type and calling child type copy constructor (try two) Brandt Dusthimer C++ 2 07-12-2006 08:35 PM
Map of Parent Type and calling child type copy constructor Brandt Dusthimer C++ 4 07-12-2006 08:19 PM
Pass from parent to child, then update parent with child value... Noel Dolan Javascript 0 07-18-2004 05:52 PM



Advertisments