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.

4
  • possible duplicate of Initializing private static members Commented Mar 16, 2015 at 8:46
  • You need to initialize outside the class: learncpp.com/cpp-tutorial/811-static-member-variables int Something::s_nValue = 1; Commented Mar 16, 2015 at 8: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. Commented Mar 16, 2015 at 9: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. stackoverflow.com/questions/11711920/… Commented Mar 16, 2015 at 10:16

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.