0

I have a question about static and static const variable in class.

Especially curious about the memory status about static and static const.

In the following code,

#include <iostream>
using namespace std;

class test{
public:
    static const int testConstStatic =1;
    static int testStatic;
};

int test::testStatic = 0;

int main(){

  cout << test::testStatic << endl;
  cout << test::testConstStatic << endl;
  return 0;
}

why does 'static int testStatic' need definition to be used and if not, I got 'undefined reference' about testStatic?

Does this definition make linkage about testStatic?

And what about testConstStatic?

Thanks in advance!

UPDATED!!

The main reason of this question was that when I declared static variable as global which surely not defined and printout then no message about 'undefined reference' BUT for the static variable in CLASS without definitino it show the message 'undefined reference'

#include <iostream>
using namespace std;

static int testStaticInGlobal;

class test{
public:
        static int testStatic;
};

int test::testStatic = 0;

int main(){

      cout << test::testStatic << endl;  // 'Undefined reference' error without definition
      cout << testStaticInGlobal << endl; // no error without definition

      return 0;
}

Thanks!

Hwangho Kim
  • 629
  • 4
  • 20
  • 1
    What is a `static const`? Just `const` will suffice – Ed Heal Apr 11 '14 at 02:52
  • @EdHeal +1 to the comment, and there are exceptional conditions. Ex. A DefaultValue constant (such as a `static const char[]`) that is compared *by address* against a member to determine whether to dyna-free it while enforcing a never-nullptr policy. Admittedly rare, but it does happen, Outside of that I couldn't agree with you more, especially in what appears to be the OP's case. (though now that I think about i, every example i've seen that does that breaks all the rules by casting the const-away). – WhozCraig Apr 11 '14 at 03:04
  • With your update, `static int testStaticInGlobal;` is a definition, while `static int testStatic;` is not because it's in a class. – chris Apr 11 '14 at 03:23
  • @chris Ok, it seems so but just because it is in the class? – Hwangho Kim Apr 11 '14 at 03:26
  • @HwanghoKim, Yes, that's the gist of it. I figure it has to do with the fact that if it was a definition, you'd run into multiple definitions way too easily. – chris Apr 11 '14 at 03:28
  • @chris You right. Maybe compiler decide whether it is "declaration + definition' or 'just declaration'...? – Hwangho Kim Apr 11 '14 at 03:32

4 Answers4

4

All variables in C++ must be defined before use. How this definition occurs is dependent on the type of variable.

  • For non-static class member variables, the definition may be implicitly performed by the class's constructor (which may itself be implicit).
  • Since static variables, by definition, are not intialized by a constructor, static member variables must always have an explicit definition.
  • For convenience, C++ allows the definition of a static const member to be combined with its declaration, as you have done, under certain circumstances. Conceptually, the static const int testConstStatic = 1; is doing two distinct things: declaring the testConstStatic member, and defining it to have a value of 1. For whatever reason, the language designers did not choose to allow these two steps to be combined for non-const static members.

Incidentally (to address Ed Heal's comment), a non-static const member (like any non-static member variable) must be defined at construction of object; it will not change after construction is completed, but it may have a different value for each instance of the class, unlike a static const member, which will always have one and only one value for the entire duration of the program.

Community
  • 1
  • 1
CarLuva
  • 550
  • 4
  • 14
  • 2
    See [this answer](http://stackoverflow.com/a/2605559/962089) for a note about that third point. It won't always work. – chris Apr 11 '14 at 03:17
  • Good point, @chris; thanks for the clarification. Answer updated. – CarLuva Apr 11 '14 at 03:25
  • Good, but getting technical, it's still not a definition, just an initialization. The definition is only ever outside, typically in one .cpp so as not to have multiple definitions because multiple translation units include that header. – chris Apr 11 '14 at 03:31
  • @CarLuva for the second point,you mentioned static needs explicit definition, then what about `static int testStaticInGlobal;`? does it include explicit definition? – Hwangho Kim Apr 11 '14 at 03:57
3

static const int members are a special case. They're treated as compile-time constants in most usage, and thus a storage location for them isn't required. The only exception is when you try to make a pointer or reference to this variable, then it needs a place to live and you'll need to provide a definition.

Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
0

static member variable needs to be defined outside the class. That's a rule.

C++11 onwards, const static members usually don't have to be defined outside the class.

Static data members are treated as global variables shared among the object instances, so they ned to be defined once only, and safe bet is outside the class.

Dr. Debasish Jana
  • 6,980
  • 4
  • 30
  • 69
-2

const variables are by definition static. So the static in static const is redundant. i.e. just const will do.

The static is required for the static int because without it, the variable is a normal non-static member variable that is only defined for the instance.

Phillip Ngan
  • 15,482
  • 8
  • 63
  • 79