static variable in the class declaration or definition?

后端 未结 4 1669
夕颜
夕颜 2020-12-01 21:07

I am new to C++.
I have a class like this:

class CodeTest
{
private:
    static const int TOTAL=100;
};

Is TOTAL a dec

4条回答
  •  醉梦人生
    2020-12-01 22:06

    static const int TOTAL=100; // is a declaration followed by an initialisation.
    

    From the C++ standard section 3.1:

    A declaration introduces names into a translation unit or redeclares names introduced by previous declarations. A declaration specifies the interpretation and attributes of these names.

    The next paragraph states that a declaration is a definition unless...... it declares a static member within a class definition:

    struct X
    {
        int a;         // defines a
        static int b;  // declares b
    };
    

    You can read more about definition and declaration here: SO: What is the difference between a definition and a declaration?

提交回复
热议问题