// SomeOtherClass.hpp
#pragma once
int someOtherCallMe();
class SomeOtherClass {
public:
static int callMe() {
static int _instance = 7;
++_instance;
return _instance;
}
};
// SomeOtherClass.cpp
#include "SomeOtherClass.hpp"
int
someOtherCallMe() {
return SomeOtherClass::callMe();
}
// main.cpp
#include "SomeOtherClass.hpp"
#include <iostream>
int
main() {
std::cout << SomeOtherClass::callMe();
std::cout << someOtherCallMe();
return 0;
}
I have three files: SomeOtherClass.hpp/cpp, main.cpp. Those files result in two binaries: shared library (of SomeOtherClass.cpp) and executable(of main.cpp, linked with shared library).
Does C++ guaranties that static <any-type> _instance will be a single variable during the execution of a program (does not matter in how many binaries it was defined)?
Note
To clarify the situation. The confusion I see in this situation is that, on one hand, the SomeOtherClass::callMe is defined in program twice, that is expected (because class static member function are actually a regular function with internal linkage, if they are defined in place, like in this case), and that is what you can see from disassembly. Since we have two functions with static local variables in machine code. How does the language/standard qualify their behaviour?
__declspecwhen building the DLL and using the DLL for it to work.