Under C++11 it is possible to initialize class members directly upon declaration. But it is also ok to initialize them once more in the initialization list of the constructor... why?
#include <iostream>
struct MyStr
{
MyStr()
:j(0)
{
std::cout << "j is " << j << std::endl; // prints "j is 0"
}
const int j = 1;
};
int main()
{
const int i = 0;
MyStr mstr;
}
Because doing something like this is an error, understandably:
MyStr()
:j(0),
j(1)
{
}
What is different about the first example, where the data member gets initialized upon declaration and then again in the constructor's init list?