Correct place to initialize class variables?

c++

Question

Where is the correct place to initialize a class data member? I have the class declaration in a header file like this:

Foo.h:

class Foo {
private:
    int myInt;
};

Then I try to set a value to myInt in the corresponding .cpp file:

Foo.cpp:

int Foo::myInt = 1;

I get a compiler error for redefining myInt. What am I doing wrong???

1
20
4/6/2009 7:57:38 PM

Accepted Answer

What you have there is an instance variable. Each instance of the class gets its own copy of myInt. The place to initialize those is in a constructor:

class Foo {
private:
    int myInt;
public:
    Foo() : myInt(1) {}
};

A class variable is one where there is only one copy that is shared by every instance of the class. Those can be initialized as you tried. (See JaredPar's answer for the syntax)

For integral values, you also have the option of initializing a static const right in the class definition:

class Foo {
private:
    static const int myInt = 1;
};

This is a single value shared by all instances of the class that cannot be changed.

32
4/6/2009 7:44:17 PM

To extend on Jared's answer, if you want to initialize it the way it is now, you need to put it in the Constructor.

class Foo
{
public:
    Foo(void) :
    myInt(1) // directly construct myInt with 1.
    {
    }

    // works but not preferred:
    /*
    Foo(void)
    {
        myInt = 1; // not preferred because myInt is default constructed then assigned
                   // but with POD types this makes little difference. for consistency
                   // however, it's best to put it in the initializer list, as above
                   // Edit, from comment: Also, for const variables and references,
                   // they must be directly constructed with a valid value, so they
                   // must be put in the initializer list.
    }
    */

private:
    int myInt;
};

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon