跳转至

Copy Constructor

约 33 个字 14 行代码

Only necessary to be written manually when there exists member of pointer type.

When Copy Ctors are called

  • Initialize from other object

    A a(b);
    A a = b; // equivalent to A a(b)
    
  • Function call by value

    void f(A a);
    
  • Function return

    A f(){
      A a;
      return a;
    }
    
    A f2(){
      return A();
    }
    A b = f(); // Copy Ctor is called
    A c = f2(); // Copy Ctor not called
    

Forbid Copy Ctor

A(const A& other) = delete;