Search code examples
c++classconstants

static const vs. const static


I do not understand the difference between these two statements in my C++ class:

class MyClass {
  public:
  private:
     static const int var = 0;            // Option 1
     const static int var = 0;            // Option 2
};

What is the difference b/w Option 1 and Option 2?? They both compile.


Solution

  • They mean exactly the same thing. You're free to choose whichever you think is easier to read.

    In C, you should place static at the start, but it's not yet required. I'm not sure if C++ followed C in this regard.

    6.11.5 Storage-class specifiers

    1 The placement of a storage-class specifier other than at the beginning of the declaration specifiers in a declaration is an obsolescent feature.