From Thinking in C++
Because static data has a single piece of storage regardless of how many objects are created, that storage must be defined in a single place. The compiler will not allocate storage for you. The linker will report an error if a static data member is declared but not defined.
The definition must occur outside the class (no inlining is allowed), and only one definition is allowed. Thus, it is common to put it in the implementation file for the class. The syntax sometimes gives people trouble, but it is actually quite logical. For example, if you create a static data member inside a class like this:
Then you must define storage for that static data member in the definition file like this:Qt Code:
class A { static int i; public: //... };To copy to clipboard, switch view to plain text mode
You have to run a level 3 diagnostic.
Ashes to ashes, Qt to Qt ( wysota )
Yes, It makes sense, I'm just wondering why my compiler doesn't require it.
And now I know why - it was optimising it out. If I do some more work with it, such as assign it to a volatile or such like, then it quite happily throws up a linker error.
You may have noticed I've never used a c++ static member variable. Static functions a plenty, but never variables.
Bookmarks