Click here to Skip to main content
15,949,686 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Hi,
I've read this somewhere:"When making a class instance, the default constructor of its fields are invoked". Is this true? I was kind of testing it, doesn't seem to work.

particularly, if I have class:

C++
class Car
{

public:

Truck truck;

Car()
{
// do nothing basically
}

}


does calling following code, automatically invoke constructor of Truck?
C++
Car car;


Even if truck is defined as a pointer, e.g.

C++
class Car
{

public:

Truck* truck;

Car()
{
// do nothing basically
}

}
Posted

1 solution

In the first case, the Truck constructor with no parameter (the default constructor) is implicitly called for the member truck.
In the pointer case, the pointer remains uninitialized. Reason: any pointer is a basic type (like an int, char, etc.), and thus, is not initialized unless explicitly told to do so (e.g. in the initializer list of a Car constructor).
Cheers
Andi
 
Share this answer
 
v2
Comments
nv3 26-Mar-13 18:53pm    
Good answer. You beat me by a second.
Andreas Gieriet 26-Mar-13 18:56pm    
Thanks and - sorry for beating you ;-)
Cheers
Andi
Dan page 26-Mar-13 19:01pm    
Hi, thanks. What happens in such case, imagine in the constructor of car I added truck = new Truck(); But inside Truck class, I also have a member variable Wheel *wheel; - but I do not initialize it in the constructor of Truck (e.g., I don't do new and just assign a NULL to it for example). wheel in such a case will remain uninitialized right? e.g., no allocation will be done for it automatically..
nv3 26-Mar-13 19:12pm    
Exactly right. There is a big difference between embedding an object in your class or just declaring a member with a pointer on it. Embedding has the advantage that you don't have to care about the allocation of the object; it is just created as part of the outer object. The down side is: Whoever wants to create the outer object must have access to the header file of the embedded object. In large projects that can make a huge difference in compile time.
Just using a pointer to the other object requires you to use new and delete for the allocation / deallocation. But the header file of your object (your Car class) doesn't need to include the header file of the other object (Truck). An advance declaration of the class name will do, like:

class Truck;

The include of the header file (Truck.h) is only required in the .cpp file. And that is indeed a big advantage.

So, consider carefully when you want to embed and when to use a pointer.
Andreas Gieriet 26-Mar-13 19:49pm    
Be careful about the meaning of "initialized", "constructed", "default constructor" and "allocated".
Initialized means that some variables/members get a defined value initially assigned.
Constructed means that the constructor of some class/struct is executed - that constructor may or may not initialize all of its members.
Default constructor is the constructor that is called if not mentioned in the initializer list or on a variable of that type (in your case, the Car constructor does not mention its members, therefore it defaults to call the respective default constructors if they exist. Basic types have no constructors, thus, no default constructors are called for such members.
Allocated means there is memory reserved on request. This is either on the pre-initialized section (e.g. static variables), on the stack (e.g. for function local variables) or on the heap (e.g. for memory requested by the C function malloc and its siblings, or by the C++ new operator). For the first two cases, the compiler calculates the needed size at compile-time. For the heap case, the size is given at run-time: malloc simply reserves that chunk of memory and returns the address to its first byte (the content remains uninitialized). new basically calls internally malloc, calls the given constructor (if it exists) on that memory and finally returns the address do that memory chunk, casted to the given type.

Class and struct types have an implicit default constructor (unless you provide your own constructors). An implicit default constructor does nothing else but calling the default constructors of its members (if they exist), etc. The other members remain uninitialized, i.e. they have random values.

Now that you know that a pointer is a basic type without (default) constructor, in Truck, the Wheel pointer does not get initialized by default (i.e. by not mentioning in the initializer list of the Truck constructor). If you want to initialize the Wheel pointer, you need to define an explicit Track constructor and initialize the Wheel pointer to either NULL (meaning, it is definied to not pointing to any instance of Wheel), or to the address of a new Wheel instance on the heap (e.g. by calling new Wheel(); and assigning that to the pointer).

BTW: you may see constructs that resemble constructor calls to basic types. This is just a convenience (and a need for template programming), but they have no default behaviour in construction (e.g. int i(17) is equivalent to int i = 17;).
Cheers
Andi

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900