Constructor

I wanna look at Constructor, i have reade it on Google.
"In C++, whenever an object of a class is created, its constructor is called. But that's not all--its parent class constructor is called, as are the constructors for all objects that belong to the class. By default, the constructors invoked are the default ("no-argument") constructors. Moreover, all of these constructors are called before the class's own constructor is called."

but i dont understand setence "But that's not all--its parent class constructor is called, as are the constructors for all objects that belong to the class"

who can explain clearly, thank so much.
1
2
3
4
5
6
class T : public U {
  W member;
  T(); // a constructor
}

T sample;

The creation of object "sample" calls constructor T::T().

Part of the object is data for type U object, because T is-a U due to inheritance.
Part of the object is data of "member".

1
2
3
4
// constructor of T
T::T()
{ // when this block starts, both U-part of the object and member have to be already initialized
}

Constructor of U initializes the U-part.
Constructor of W initializes the "member".
Last edited on
thank you so much. when i create a obj T(10),
if U have U:U(int) and i create a constructor T:T(int value) : U(value) by inlitializer list then constructor U will initializes the U(int) part into that T type, that is right ?
Yes.

Note that
1
2
3
T::T()
: U( 42 )
{}

is valid too, if it makes sense to have 42 in the U-part of a default T.
Topic archived. No new replies allowed.