In C++03, only const static members of integral type can be initialised directly in the class.
C++11 allows members to be initialised in-class.
class Test
{
public:
const static int x = 0; // C++03
int y = 0; // C++11 only
};
We can also initialise objects e.g. strings:
class Test
{
public:
std::string str = "Hello";
};
To use the non-default constructor, curly brackets can be used to specify the parameters, e.g.:
class Test
{
public:
std::string str{"Hello"};
};
For classes with multiple constructors, in-class initialisation is particularly useful as it can avoid duplication of member initialisation data.