1

I'm new to C++ and still in learning phase, so this may be a simple and probably dumb question to you ;(

From other questions and answers on the board, I learned that it is customary and preferred to initialize the private static class data member in the cpp file along with the other member function definitions.

However, could it be possible to initialize the member function as global variable in main.cpp? Since all objects should share one static data member, why not just initialize it there? (I would think initializing it in main itself, but I would guess this would spit out a compilation error)

Could you please explain if this is technically not plausible or just not done conventionally. Since the static data member is initialized in class cpp file as a global variable anyways, I do not see a reason why initializing it in main cpp will fail. Please advise.

Brian Tompsett - 汤莱恩
  • 5,195
  • 62
  • 50
  • 120
GrinNare
  • 125
  • 8
  • possible duplicate of [Initializing private static members](http://stackoverflow.com/questions/185844/initializing-private-static-members) – xmoex Mar 16 '15 at 08:46
  • You need to initialize outside the class: http://www.learncpp.com/cpp-tutorial/811-static-member-variables/ `int Something::s_nValue = 1;` – Binkan Salaryman Mar 16 '15 at 08:46
  • Think twice before using static members, unless they are const. Pitfalls you may come across include SIOF, and race conditions. The latter only applies when you or someone else want to do things in parallel with your class. – user877329 Mar 16 '15 at 09:00
  • Handling of statics, in particular function-static variables and other statics used for singleton idioms, has improved with C++11. Before, you basically needed to mutex-protect everything yourself, now the compiler helps you. See e.g. http://stackoverflow.com/questions/11711920/how-to-implement-multithread-safe-singleton-in-c11-without-using-mutex – Erik Alapää Mar 16 '15 at 10:16

1 Answers1

1

Suppose following header file class.hpp

#pragma once // sort of portable
struct C // to make the example shorter.
{
    static int answer;
};

and following source file class.cpp

#include "class.hpp"
// nothing here

and following main source file main.cpp

#include <iostream>
#include "class.hpp"
int C::answer = 42;
int main()
{
    std::cout << "And the answer is " << C::answer << "." << std::endl;
}

Now, compile class.cpp -> class.obj, main.cpp -> main.obj, and link class.obj main.obj -> executable. It works as expected. But suppose, you came up with different project (anothermain.cpp), that will use same class.hpp.

#include "class.hpp"
int main()
{
    std::cout << "And the answer is " << C::answer << "." << std::endl;
}

Going through same compilation process results in link error

unresolved external symbol "public: static int C::answer"

So, to answer your question. It is possible. The linker does not care which object file contains definition for that value (as long as it's defined only once). However, I would not recommend it.

Zereges
  • 4,833
  • 1
  • 20
  • 47