Home > OS >  when "B c = a" why the output isnt : "cosntructor A\n constructor B\n copy construc
when "B c = a" why the output isnt : "cosntructor A\n constructor B\n copy construc

Time:04-05

Output:

when B c = a why the output isnt :

cosntructor A
constructor B
copy constructor B

instead of

cosntructor A
copy constructor B

?

======================================================================================== CODE

class A {

public:
    A(const A&);
    A();
    ~A();
};

class B : public A {
public:
    B(string, int, float, int);
    B(const B&);
    B();
    ~B();
};

A::A() { cout << "constructor A\n"; }
A::A(const A& old_str) { cout << "copy constructor A\n"; }
A::~A() { cout << "destructor A\n"; }

B::B() { cout << "constructor B\n"; }
B::B(const B& old_str) { cout << "copy constructor B\n"; }
B::~B() { cout << "destructor B\n"; }

int main()
{
    B a;
    cout << "\n\n\n";
    B c = a;
    cout << "\n\n\n";
}

I don t understand when "B c = a" for c aren t called both constructors, of A and B. For B c the output is constructor A constructor B which is fine, why isn t the same happening for "B c = a"

CodePudding user response:

You don't see "copy constructor A" printed because you don't copy construct the A base in the B copy constructor. You can do that in the member-initialiser-list.

B::B(const B& other) : A(other) { cout << "copy constructor B\n"; }

See it live

CodePudding user response:

when B c = a why the output isnt :

Because B c = a; is copy-initialization. Meaning c is created as a copy of object a, using the copy constructor B::B(const B&). So the copy constructor B::B(const B&) is implicitly called by the compiler due to the statement B c = a;. Now, before entering the body of this copy constructor of B, the default constructor A::A() is also implicitly called. Hence you get the output:

constructor A
copy constructor B.

On the other hand, B a; is default initialization which uses the default constructor B::B(). So the default constructor B::B() is implicitly called due to the statement B a;. But before entering the body of this default constructor of B, the default constructor A::A() is also implicitly called. Hence you get the output:

constructor A
constructor B

CodePudding user response:

In this declaration

B c = a;

there is used the defined by the user the copy constructor of the class B.

B::B(const B& old_str) { cout << "copy constructor B\n"; }

The copy constructor implicitly invokes the default constructor of the base class A to create its base class sub-object. As a result you have

cosntructor A
copy constructor B

In fact the copy constructor of the class B

B::B(const B& old_str) { cout << "copy constructor B\n"; }

is equivalent to

B::B(const B& old_str) : A() { cout << "copy constructor B\n"; }

The reason of your confusing is that it seems you think that in this declaration

B c = a;

at first there is created the object c using the default constructor of the class B and then the object a is assigned to the created object c using one more constructor: the copy constructor.

However only one constructor can be used to crate an object and in this case there is used the copy constructor of the class B.

In this declaration

B c = a;

there is neither assignment. a is an initializer that initializers the created object c. You could rewrite this declaration also the following way making it more clear

B c( a );
  • Related