3

If(and only if) you use an initialized member in a way that requires it to be stored as an object in memory, the member must be (uniquely) defined somewhere.

from "The C++ Programming Language"

I have a class

class Bingo{
      std::string name;
    public:
      Bingo(){}
      int i;
      static const int i89=89;
};

and I don't need to have definition like:

const int Bingo::i89;

which is described as necessary. Therefore I don't understand apparently. Could you explain the meaning of that quotation please?

Community
  • 1
  • 1
4pie0
  • 29,204
  • 9
  • 82
  • 118
  • Changed visual-c++ tag to c++, there is nothing visual-c++ specific in this, it is c++. – Alok Save Nov 26 '12 at 04:00
  • I think it is related to ODR. Similar question is present at http://stackoverflow.com/questions/4547660/c-static-member-variable-and-its-initialization. You can check it for information. – kumar_m_kiran Nov 26 '12 at 05:00

2 Answers2

2

When you define a member inside the class it is known as In-class Initialization.

Note that such members can be treated as compile time constants by the compiler because it knows that the value will not change anytime and hence it can apply its own optimization magic and simply inline such class members i.e, they are not stored in memory anymore. Since they are not stored in memory one cannot take the address of such members.The vice versa applies.

The above follows from Bjarne's rationale that each C++ object needs unique definition and hence each object needs to be stored in memory so that they can have unique address and be uniquely identified.

Hence the quote,

If(and only if) you use an initialized member in a way that requires it to be stored as an object in memory, the member must be (uniquely) defined somewhere.

Alok Save
  • 202,538
  • 53
  • 430
  • 533
  • there is an example with int, similar to what I have written in my example and int definition out of the class is described as necessary. – 4pie0 Nov 26 '12 at 03:38
  • 1
    @cf16: Because the program somewhere probably does something like `&Bingo::i89`, i.e: takes address of the member. – Alok Save Nov 26 '12 at 03:40
  • the example in the book takes this address, true, but I have tried that even then I don't need it to be defined – 4pie0 Nov 26 '12 at 04:17
  • 1
    @cf16: Because the new c++11 standard changed these rules. The book seems to be referring to the pre C++11 standard, wherein these were the rules. [This](http://stackoverflow.com/a/9657064/452307) old answer of mine might clear things out a bit more for you. – Alok Save Nov 26 '12 at 04:31
  • no, it is in C++98 as __cplusplus macro in my implementation expands to 19971L. – 4pie0 Nov 26 '12 at 05:01
  • @cf16: Well, the rules have changed in c++11. Are you sure you are compiling using c++11 or anything before that? – Alok Save Nov 26 '12 at 05:05
0

As far as I remember you need :: to access the static variable when it is defined as it is in your class

ashcliffe
  • 138
  • 6
  • the question is why I don't need const int Bingo::i89; – 4pie0 Nov 26 '12 at 03:07
  • @cf16 because you don't use it "in a way taht requires it to be stored as an object in memory" – lenik Nov 26 '12 at 03:08
  • there is an example with int, similar to what I have written in my example and int definition out of the class is described as necessary. – 4pie0 Nov 26 '12 at 03:38