Skip to main content
gamedev.net
🔒 Locked

Default constructor

Started by The C modest god 4 replies 350+ views
Original Post
The C modest god
The C modest god
Does the default class_name() constructor calls all the () constructors of its sub class?
It's all about the wheel.Never blindly trust technoligy.I love my internal organs.Real men don't shower. Quote: Original post by Toolmaker Quote:
Nervo
Nervo
No it shouldn't. Any nested classes should have its own default constructor.

EDIT: Um, that was not what you were asking when I reread your post. I believe you can do what you are asking though.

[edited by - nervo on December 30, 2003 1:17:32 PM]
Well, R2D22U2..
daerid
daerid
Constructors and Destructors are called like a stack. When you create an instance of a class that is derived from another class (or classes), then the Constructor of the base class is called first. Destructors are the opposite: The derived destructor is called first, and then the the base class destructor.

For example, say you had a class hierarchy like so:

Object -> Entity -> NPCEntity -> Monster

Given the following line of code:

Monster* badGuy = new Monster();

this is what would happen:
Object::Object();
Entity::Entity();
NPCEntity::NPCEntity();
Monster::Monster();

And given this line:

delete badGuy;

this is what would happen:
Monster::~Monster();
NPCEntity::~NPCEntity();
Entity::~Entity();
Object::~Object();

Hope that helped out.


daerid | Legends | Garage Games | Spirit | Hapy | Boost | Python | Google
"Doomed to crumble, unless we grow, and strengthen our communication" - Maynard James Keenan, Tool
daerid@gmail.com
Fruny
Fruny
Unless you specify otherwise in the constructor initialisation list, any constructor will call the default constructor for the base classes.

Derived::Derived() {};           // default Base constructor called
Derived::Derived(int, int) {}; // default Base constructor called
Derived::Derived() : Base(1000) {}; // Base(int) constructor called


If Base itself derive from some other class, Base''s constructors decide how that other class gets constructed.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
psykr
psykr
If I had the following class hierarchy: Object -> Entity -> Monster
If Entity's default constructor was Entity::Entity() : Object( 100 ), and Monster's constructor called Monster::Monster() : Object( 200 ), what would happen?

edit: Ah, after experimentation I see that you can't call Object( 200 ) from Monster's constructor.

[edited by - psykr on December 30, 2003 1:34:58 PM]
Fruny
Fruny
You would get a compile time error. Object is not an immediate base class of Monster.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.