Monday, July 31, 2017

C++11: =default

Instead of providing a body for a class constructors or assignment operators, you can specify =default in the declaration. Here is an example:
#include <iostream>
class A
  A() = default; // Without this line, this file will not compile.
  A(const A& a)      {std::cout << "Copy CTOR called; ";}
  A& operator=(A& a)
    std::cout << "Assignment Operator called; ";
    return *this;
int main()
  A a1;
  A a2(a1); // Copy CTOR called
  a1 = a2;  // Assignment Operator called
  return 0;
// Output: Copy CTOR called; Assignment Operator called;


No comments:

Post a Comment