0

In the below code, the output is "SFunction()". I am not finding a logical reasoning as to how this is allowed. I am compiling it using "g++ --std=c++11 references_stack_overflow.cpp"

struct S{
    S(){std::cout<<"Constructor"<<std::endl;}
    void SFunction(){std::cout<<"SFunction()"<<std::endl;}
};

void functionTakingReference(S& ref)
{
    ref.SFunction();
}

int main() {
    S* obj_ptr = nullptr;
    functionTakingReference(*obj_ptr);
    return 0;
}
Vikki.I
  • 1
  • 1
  • 1
    Kindly someone close this as duplicate of [Calling class method through NULL class pointer](http://stackoverflow.com/q/2505328/514235) – iammilind Apr 24 '16 at 03:05
  • 2
    You have *undefined behavior*. One of the outcomes of that is that it might *seem* to work fine. Try adding a member variable and write to it in the function and it should crash. – Some programmer dude Apr 24 '16 at 03:07

0 Answers0