Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Why was the copy constructor called?

class A {
public:
    A() {}
    A(const A& a) { cout << "A::A(A&)" << endl; }
};

class B {
public:
    explicit B(A aa) {}
};

int main() {
    A a;
    B b(a);
    return 0;
}

Why does it print "A::A(A&)"?

When was the copy constructor for "A" called? And if the code calls the copy constructor, why can I remove the copy constructor without creating a compilation error?

like image 251
Jared Avatar asked Nov 29 '22 08:11

Jared


1 Answers

B(A aa) takes an A by value, so when you execute B b(a) the compiler calls the copy constructor A(const A& a) to generate the instance of A named aa in the explicit constructor for B.

The reason you can remove the copy constructor and have this still work is that the compiler will generate a copy constructor for you in cases where you have not also declared a move constructor.

Note: The compiler generated copy constructor is often not sufficient for complex classes, it performs a simple member wise copy, so for complex elements or dynamically allocated memory you should declare your own.

§ 15.8.1

If the class definition does not explicitly declare a copy constructor, a non-explicit one is declared implicitly. If the class definition declares a move constructor or move assignment operator, the implicitly declared copy constructor is defined as deleted; otherwise, it is defined as defaulted (11.4). The latter case is deprecated if the class has a user-declared copy assignment operator or a user-declared destructor or assignment operator.

like image 75
lcs Avatar answered Dec 05 '22 18:12

lcs