473,396 Members | 1,754 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Calling parent constructor from child constructor

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!
Jul 22 '05 #1
3 3510
* 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?
Jul 22 '05 #2
mo*******@hotmail.com (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" | \|__| |
| pa****@sbox.tugraz.at | ICQ #320057056 | |
|-------------------University of Technology, Graz----------------|
Jul 22 '05 #3

"pantalaimon" <mo*******@hotmail.com> wrote in message
news:fe************************@posting.google.com ...
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
Jul 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: pablo | last post by:
Dear NewsGroupers, I am relatively new to OOP and cannet get my head around this problem. I have two classes. Class Child extends Parent. There is no constructor for the Child class. So when I...
2
by: jerrygarciuh | last post by:
Hello, Is it possible to instantiate a child class within the constructor of its parent? eg Class DBI extends DB { function DBI() { // explicit parent constructor call
14
by: Axel Straschil | last post by:
Hello! Im working with new (object) classes and normaly call init of ther motherclass with callin super(...), workes fine. No, I've got a case with multiple inherance and want to ask if this...
2
by: William Payne | last post by:
Hello, consider these following two classes. A base class, class MDIChildWindow, and a class inherting from that base class, class Document. In the static base member function callback() I obtain a...
6
by: Jon Hyland | last post by:
Ok, I'm a little rusty on this, it should be a simple problem but I can't figure it out. How can I handle form events in my main code page?? I'm creating a Windows App in C#. Rather than make...
1
by: Mickey Swanson | last post by:
I have a MDI app wich has several different forms I need to call a method on one MDIchild form from another child form. I have a MDI form called frmMDI. I have a mdichild form called frmMain....
10
by: Goran Djuranovic | last post by:
Hi all, Does anyone know how to declare a variable in a class to be accessible ONLY from a classes instantiated within that class? For example: ************* CODE ***************** Public...
6
by: Sashi | last post by:
class parent{ Parent(){}; ~Parent(){}; } Child: public Parent{ Child(){}; ~Child(){};
1
by: =?Utf-8?B?cmFuZHkxMjAw?= | last post by:
The code below is pretty simple. Calling Talker() in the parent returns "Parent", and calling Talker() in the child returns "Child". I'm wondering how I can modify the code so that a call to the...
11
dlite922
by: dlite922 | last post by:
If I have class Parent { function __construct() { die("I'm In Parent"); }
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.