Take the 2-minute tour ×

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{

    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?

share|improve this question
Do all objects of type foo use the same const values? x=1 y=2 z=3 –  olibre Nov 17 '12 at 23:00
Can you use specific C++11 features? (be caution: if your code uses a C++11 feature, then your code may not compile on many current C++ compilers) –  olibre Nov 17 '12 at 23:03
add comment

3 Answers

up vote 4 down vote accepted

make these values static for the class, this way all object will inherit these same values.

static const int x = 1;
static const int y = 2;
static const int z = 3;

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.

#include "foo.h"
#include <//libs....>
int foo::x;
int foo::y;
int foo::z;
//class functions down below
share|improve this answer
static const or static constexpr is better. –  Pubby Nov 17 '12 at 22:47
he never specified if he wanted the values to remain constant, he might wish to modify the values at a later point in the program. –  Gmercer015 Nov 17 '12 at 22:47
If I recall correctly, static const int can be initialized in the class declaration. –  Karolis Juodelė Nov 17 '12 at 22:47
@Need4Sleep: "[...] that I want to always be the same value NO MATTER WHAT." –  Mitch Nov 17 '12 at 22:50
the title specify he wants it constant –  emartel Nov 17 '12 at 22:50
show 7 more comments

You can also use an initializer list in the constructor to set these fields' initial values, just as with any other member:

class foo {
    const double x;
    const double y;
    const double z;

    foo() : x(1), y(2), z(3) {
share|improve this answer
add comment

You don't have to make them static. You could declare them like this:

class foo{

    double var_1, var_2, var_3;
    const double x=1.0;
    const double y=2.0;
    const double z=3.0;

[functions go here]


Though if they are integer values then declaring them as ints would be better.

share|improve this answer
That's a C++11 feature for non-int types –  Flexo Nov 17 '12 at 22:53
Actually, I'm going to downvote this for the time being since OP clearly is not using C++11 and so this won't work. –  Pubby Nov 17 '12 at 22:54
@Pubby: What makes you so sure? –  Lightness Races in Orbit Nov 18 '12 at 0:07
@LightnessRacesinOrbit Since OP posted almost identical code. The only difference is that OP's is all on one line. (This answer's original explanation was that he could not initialize multiple variables on one line which was incorrect) –  Pubby Nov 18 '12 at 0:11
@Pubby: That's a good reason. –  Lightness Races in Orbit Nov 18 '12 at 0:49
add comment

Your Answer


