What will happen when I call a member function on a NULL object pointer?

后端 未结 6 1621
闹比i
闹比i 2020-11-22 01:56

I was given the following as an interview question:

class A
{
public:
    void fun()
    {
        std::cout << "fun" << std::endl;
             


        
6条回答
  •  生来不讨喜
    2020-11-22 02:44

    It's undefined behavior, so anything might happen.

    A possible result would be that it just prints "fun" since the method doesn't access any member variables of the object it is called on (the memory where the object supposedly lives doesn't need to be accessed, so access violations don't necessarily occur).

提交回复
热议问题