Monday, July 31, 2017

C++11: Delegating Constructors

You can now call a constructor from the MIL (Member Initialization List) of another constructor from the same class.
#include <iostream>
class A
{
 public:
  int m_a;
  A() : A(42) {}
  A(int a) : m_a(a) {}
};
int main()
{
  A a;
  std::cout << a.m_a <<  std::endl;
  return 0;
}
// Output: 42
Reference: https://isocpp.org/wiki/faq/cpp11-language-classes#delegating-ctor

No comments:

Post a Comment