Copy Constructors

Back

Examples

Standard

class A {
  int a
  public:
    A(A& b) { this->a = b.a };
}

Copy ctor with initializer list

class A {
  int a
  public:
    A(A& b): a(b.a) {};
}

Automatically creating a contructor

class A {
  public:
    A(A& b) = default; // Force compiler to create a copy contructor
}

Preventing copy ctor creation

class A {
  public:
    A(A& b) = default; // Force compiler to not create a copy ctor
}

Illegal copy contructor

class A {
  private:
    A(A& b) {}; // Making the copy contructor illegal
}

References