class A
{
private:
int x ;
int &y ;
static int z ;
};
How can initialize these 3 variable in this class
class A
{
private:
int x ;
int &y ;
static int z ;
};
How can initialize these 3 variable in this class
The normal variable can be initialized in the constructor, in its member initializer list, but you don't have to:
A::A(int i) :
x(i) { /* c'tor */ }
The reference can only be initialized from the constructor, and you must initialize it:
A::A(int& r) :
y(r) { /* c'tor */ }
Now the y field of A will reference to the same variable as r, it is not a copy of it.
The static variable must be initialized outside the class, probably in a separate source file, you must do this if you want access that static variable.
int A::z = 0;