The default copy constructor of about c ++

copy constructor

"Inside the c ++ object model" is a very good book is especially helpful for understanding the construction and destruction of the class.

If one is following me about c ++ default copy constructor; generally we use it in the following cases

// class A {...};
A aa; // default ctor
A a = aa; // copy ctor

//
void func(A a) { // TODO }
func(a) // copy ctor
    
A f() { return A(); }
f(); // copy ctor
    
// 以下是我遇到与下面相似问题时的产生的这个思考
class B : public A {
  B(const A &a) A(a) {...}  // A没有explicit copy constructor
  // TODO
};

When I did not define your own copy constructor in A, the class will use the automatically generated default copy constructor, then the default behavior of the function generated what is it?

In fact, here is a default memberwise initialization things, is to copy the value of each data member in the past, (seemingly encounter pointer, the situation will appear shallow copy), but he does not take one of the member class object, but recursively embodiment memberwise initialization manner.

Guess you like

Origin www.cnblogs.com/codemeta-2020/p/12634002.html