C++ & initializing a constant variable

hi, i created a class, with [blue]const static int x = 100;[/blue] as privete member. When compiling, my VC++6.0 says that [red]error C2258[/red], and when moving the initilzation into the header file I get[red]error C2166[/red]. Where do I initalize???

Comments

  • [b][red]This message was edited by HackmanC at 2003-9-30 14:54:21[/red][/b][hr]
    : hi, i created a class, with [blue]const static int x = 100;[/blue] as privete member. When compiling, my VC++6.0 says that [red]error C2258[/red], and when moving the initilzation into the header file I get[red]error C2166[/red]. Where do I initalize???
    :

    Compiler Error C2258
    illegal pure syntax, must be '= 0'
    A pure virtual function is declared with incorrect syntax.

    Example

    class A
    {
    public:
    void virtual func1() = 1; // error, not = 0
    void virtual func2() = [red]0[/red]; // OK
    };

    Compiler Error C2166
    l-value specifies const object

    Code attempts to modify an item declared const. The following sample generates C2166:

    // C2166.cpp
    int main() {
    int j = 0;
    const int i = 4; // i is const
    j = i; // ok
    i = 5; // C2166
    }

    So, if you declare a variable const you should not change its value in the lifetime of the software. And if you declared it like a member variable you should initialize it to 0 and change the value after in a constructor and OF COURSE dont declare it const.

    __gc class M {
    public:
    static int s_i;
    static M() : s_i(0) {}; // error: ctor-initializer
    static const int s_k; // error: needs initializer
    [red]static const int s_j=10; // ok[/red]
    };

    This is another example using const in a PUBLIC declaration of a class, maybe your overloading the var X with a function!?.

  • Hi, this is my class:
    [code]
    Class myClass
    { public:
    myClass();

    private:
    const static int x = 0; // error here, no error if const static int x;
    }

    [/code]

    so, you mean that I CANNOT use [blue]const static int x[/blue] as a member???
  • : Hi, this is my class:
    : [code]
    : Class myClass
    : { public:
    : myClass();
    :
    : private:
    : const static int x = 0; // error here, no error if const static int x;
    : }
    :
    : [/code]
    :
    : so, you mean that I CANNOT use [blue]const static int x[/blue] as a member???
    :

    ===========================
    Static Const members
    Details regarding how to initialise static const members changed late in the C++ specification. A static const data member of integral or enum type can be initialized in the class definition, but you still need to provide another definition (without an initializer) for it. So
    class C {
    const static int csi = 5;
    };

    const int C::csi;

    is legal, but you can't do the same for floats, non-statics or non-consts. With some old compilers you have to initialise outside the class.
    ===========================
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories