Saturday, 24 August 2013

Uniform Initialization and default constructor arguments in C++11

Uniform Initialization and default constructor arguments in C++11

I am learning about new C++11 feature - uniform initialization. Wrote
small program:
#include <iostream>
using namespace std;
class C {
public:
C(int a = 1, int b = 2) : a_{a}, b_{b}, n{0,1,2,3,4} {};
int n[5];
int a_,b_;
};
int main()
{
C c = C{}; // should call C(int a = 1, int b = 2) with default arg.
cout << c.a_ << " " << c.b_ << endl;
return 0;
}
However, I am getting unexpected result 0 0. In other words, everything is
initialized to zero. The only way this could have happened: 1. Implicit
default constructor was called, 2. Initialization was not done correctly.

No comments:

Post a Comment