33598

Declaring and initializing a static int in a header

Question:

If I have the following in a header file:

Foo.h

Foo { public: static const int BAR = 1234; ... };

Do I also need to define the variable in the .cpp, e.g.:

Foo.cpp

const int Foo::BAR;

We have an issue where initializing a static in a header seems to work on MS compilers but with gcc on the Mac it seems to give linker errors.

Answer1:

You need both the declaration and the definition, just as you've written them.

Since it is an integer, you can initialise it in the declaration as you've done, and the compiler should treat it as a compile-time constant when it can. But it still needs one (and only one) definition in a source file, or you'll get link errors when it can't be treated as a constant.

Apparently, Microsoft decided that the standard behaviour was too confusing, and "extended" the language to treat a declaration with an initialiser as a definition; see <a href="http://connect.microsoft.com/VisualStudio/feedback/details/379496/static-const-member-variable-defined-multiple-times" rel="nofollow">this issue</a>. The result is that you get link errors (multiply defined symbols) if you also define the symbol correctly. You can get the standard behaviour by disabling language extensions (/Za).

Answer2:

The first fragment works for some environments but the definition really is required by some compilers and of course if you take the address of your constant.

If you don't like to have to touch header and body to introduce the constant, there still is the old enum-trick:

class A { public: enum { someconstant=1234 }; };

makes someconstant available as a compile time constant without the need of a definition in the body.

Answer3:

Declarations should be done in the headers and initializations should be done on the .cpp

There's an interesting article about static member variables <a href="http://www.learncpp.com/cpp-tutorial/811-static-member-variables/" rel="nofollow">here</a>.

Answer4:

The header file

Foo { public: static const int BAR; ... };

The code file

const int Foo::BAR = 1234;

Recommend

  • Strange multiple assignment error in C++
  • GLFW triangle will not change color
  • Implementing HMAC-SHA256 for Keybase in Javascript
  • Objective C - Create a framework for my iphone apps?
  • Is C++ compilable with OpenMP and boost on MacOS?
  • addressing in assembler
  • hslogger & Duplicate Log Lines
  • What is the likely cause of a net::ERR_CONNECTION_ABORTED when uploading a file to Spring
  • Which browser have this strange user agent? (IOS device)
  • Using an STL Iterator without initialising it
  • XNU incudes in Kext
  • In C what exactly happens if i use () to initialize a double dimension array instead of the {}?
  • What Makes These Two Array Adds Different?
  • JSR-330 support in Picocontainer : @Inject … @Named(\"xxx)
  • Struts 2 TextField Tag with an attribute and no value
  • Salesforce Different WSDL files and when to use
  • NUnit 3.0 TestCase const custom object arguments
  • Z3: Convert between FP and BitVector?
  • How to revert to previous XCode version?
  • Insert new calendar with SyncAdapter- Calendar API Android
  • Install PHP intl extension on MacOS
  • Date Conversion from yyyy-mm-dd to dd-mm-yyyy
  • Groovy: Unexpected token “:”
  • Ensure fsync did its job
  • Make VS2015 use angular-cli ng at build time in a .NET project
  • VSO Build — Response status code does not indicate success: 404 (Not Found)
  • Linq Objects Group By & Sum
  • Read text file and split every line in MSBuild
  • Optimizing database types to compact database (SQLite)
  • Counter field in MS Access, how to generate?
  • Does CUDA 5 support STL or THRUST inside the device code?
  • Where to put my custom functions in Wordpress?
  • Convert array of 8 bytes to signed long in C++
  • Build own AppleScript numerical error handling
  • Numpy divide by zero. Why?
  • Buffer size for converting unsigned long to string
  • Qt: Run a script BEFORE make
  • reshape alternating columns in less time and using less memory
  • How can i traverse a binary tree from right to left in java?
  • How to Embed XSL into XML