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!

6
  • 1
    What is a static const? Just const will suffice
    – Ed Heal
    Commented Apr 11, 2014 at 2: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
    Commented Apr 11, 2014 at 3:04
  • With your update, static int testStaticInGlobal; is a definition, while static int testStatic; is not because it's in a class.
    – chris
    Commented Apr 11, 2014 at 3:23
  • @chris Ok, it seems so but just because it is in the class? Commented Apr 11, 2014 at 3: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
    Commented Apr 11, 2014 at 3:28

4 Answers 4

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.

4
  • 2
    See this answer for a note about that third point. It won't always work.
    – chris
    Commented Apr 11, 2014 at 3:17
  • Good point, @chris; thanks for the clarification. Answer updated.
    – CarLuva
    Commented Apr 11, 2014 at 3: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
    Commented Apr 11, 2014 at 3:31
  • @CarLuva for the second point,you mentioned static needs explicit definition, then what about static int testStaticInGlobal;? does it include explicit definition? Commented Apr 11, 2014 at 3: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.

1
  • @Mark Ransom Right, when I cout to print the address of 'static const' out then it show 'undefined reference' Commented Apr 11, 2014 at 3:39
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.

2
  • Just rule? Any explanation? Commented Apr 11, 2014 at 2:56
  • @HwanghoKim, It needs a definition (as in declaration vs definition). It doesn't currently have one.
    – chris
    Commented Apr 11, 2014 at 2:57
-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.

4
  • 5
    This isn't true. It's perfectly fine to have a const member that is different for each instance.
    – chris
    Commented Apr 11, 2014 at 2:59
  • 1
    followup on @chris comment, such a non-static member requires initialization as well.
    – WhozCraig
    Commented Apr 11, 2014 at 3:08
  • 1
    @WhozCraig - Via the initialisation list is the only way to do this
    – Ed Heal
    Commented Apr 11, 2014 at 3:27
  • @Ed yuppers, something held in common with reference members (const or otherwise).
    – WhozCraig
    Commented Apr 11, 2014 at 3:31

Not the answer you're looking for? Browse other questions tagged or ask your own question.