I have two classes A and B such that A has a static B instance as its member. B has a function Show() and here is my class A:
class A
{
A()
{
_b.Show();
}
private:
static B _b;
};
and subsequent code is
A a;
B A::_b;
int main()
{
}
Now B::Show() called before B is constructed due to the sequence in which I have defined
a and _b. But how does this work exactly i.e. how is it possible to make calls on an object that is still not constructed ?
It’s not possible, it’s undefined behavior (in this case, because you’re accessing an uninitialized object) because
ais initialized beforeA::_b.Look up static initialization order fiasco. You don’t get an error because 99% of the times this happens, it’s not easily diagnosable.