I’m constructing a class where I have three member variables that I want to always be the same value NO MATTER WHAT.
I have
class foo{
public:
double var_1, var_2, var_3;
double x=1, y=2, z=3;
[functions go here]
};
that gave me an error since I can’t initialize a variable like that. But I want x, y and z to always be 1, 2 and 3 respectively. I tried defining them outside the class but that doesn’t work since I want them to be member variables of the class.
How do I do this?
make these values static for the class, this way all object will inherit these same values.
through technically, this does not define the variable. If a static data member is of const integral or const enumeration type, you may specify a constant initializer in the static data member’s declaration. This constant initializer must be an integral constant expression. Note that the constant initializer is not a definition. You still need to define the static member in an enclosing namespace.