Here's my question with sample code.
In the case a class function never uses instance related data or never references this
, could the compiler decide this function is kind of static ?
As an example see this code (with description below) :
class Dummy
{
public:
Dummy()
{
std::cout << "Dummy()" << std::endl;
mInstance = this;
}
~Dummy()
{
std::cout << "~Dummy()" << std::endl;
mInstance = nullptr;
}
static Dummy& instance()
{
std::cout << "static Dummy& instance()" << std::endl;
return *mInstance;
}
void foo()
{
std::cout << "bar" << std::endl;
}
private:
int mData;
static Dummy* mInstance;
};
Dummy* Dummy::mInstance = nullptr;
- At construction, my
Dummy
class references it's instance in a static variable calledmInstance
using a pointer. mInstance
is available through the static functioninstance()
.foo()
is not a static function. Hence I need and instance to call it.mInstance
is initialized tonullptr
.
That case if my main
only does :
Dummy::instance().foo();
I should get a seg fault. Because instance() would de-reference a nullptr
.
Instead the program runs and the output will be :
bar
...And only this line so it does not seem to call neither the constructor nor the destructor.
Yet if my foo()
function is :
void foo()
{
mData = 5;
std::cout << "bar" << std::endl;
}
Here a seg fault occurs.
I tend to guess as foo()
actually did not reference anything proper to the instance, the compiler guessed it was a static and compiled this way.
But it does not seem to be logical as instance()
function returns a reference to an instance and even if the foo()
is static, foo()
should not be callable...
As an other example, without the mData = 5
instruction, if my main is :
Dummy& vDummy = Dummy::instance();
std::cout << &vDummy << std::endl;
vDummy.foo();
The output is :
statci Dummy& instance()
0
bar
I don't really understand the way things are done here.... :/