user3160866
user3160866

Reputation: 387

Initialize Object as member of another class

I just want to understand the flow how it happens as am new to c++.

Let me elaborate my problem statement.

class test1 {
public:
    test1() {
        cout << " test1::constructor called" << endl;
    }
    ~test1() {
        cout << " test1::destrcutor called" << endl;
    }
};

class test2 {
public:
    test2() {
        cout << " test2::constructor called" << endl;
    }
    ~test2() {
        cout << " test2::destrcutor called" << endl;
    }
};

class derived :public test1, public test2 {
    test2 t2;
    test1 t1;
public:
    derived() {
        cout << " derived constructor called" << endl;
    }
    ~derived() {
        cout << "derived destructor called" << endl;
    }
};

int main() {
    derived d;
    return 0;
}

The output of the above program shows

   test1::constructor called
   test2::constructor called
   test2::constructor called
   test1::constructor called
   derived constructor called
   derived destructor called
   test1::destrcutor called
   test2::destrcutor called
   test2::destrcutor called
   test1::destrcutor called

So here my question is at what points it called the constructor of the member variables in derived class as I have not put any initializer for the same.

Upvotes: 3

Views: 976

Answers (1)

mksteve
mksteve

Reputation: 13073

The order of construction is bases, then members so :-

test1::constructor called  << first base of 'derived'
test2::constructor called  << second base of 'derived'
test2::constructor called  << member of 'derived' t2
test1::constructor called  << member of 'derived' t1
derived constructor called << code in 'derived::derived'
derived destructor called  << code in 'derived::~derived'
test1::destrcutor called   << destruction of t1
test2::destrcutor called   << destruction of t2
test2::destrcutor called   << destruction of derived
test1::destrcutor called   << destruction of derived

There is only one destructor of an object, and it has a defined order to destroy objects. That is destroy all the members from bottom to top of the class.

Then destroy the class, and its bases in "reverse order".

Each constructor can choose what to initialize, but not the order.

a_class::a_class( params ) : 
           base_n( params ), base_n_1( params ), 
           member_1( params), member_2( params),...

This member initialization list allows different parameters to be given to construct all the bases and objects, but does not effect the order. It is always first_base, second_base, first_member, second_member, ...

This ordering is to ensure it is the opposite of the destructor.

These rules allowed me to work out which message was from the members, and which from the bases.

A member which does not get initialized from the member initialization list will still get its default constructor called test2::test2. As once a class/struct have a constructor, they will only come into existence by having a constructor called.

Plain-old-Data or POD are simple types such as int which don't have constructors. They are left uninitialized (whatever values were left behind in the memory).

Upvotes: 8

Related Questions