Accessing class members on a NULL pointer

后端 未结 8 1605
孤城傲影
孤城傲影 2020-11-22 14:11

I was experimenting with C++ and found the below code as very strange.

class Foo{
public:
    virtual void say_virtual_hi(){
        std::cout << \"Vi         


        
8条回答
  •  一整个雨季
    2020-11-22 15:14

    The say_hi() member function is usually implemented by the compiler as

    void say_hi(Foo *this);
    

    Since you don't access any members, your call succeeds (even though you are entering undefined behaviour according to the standard).

    Foo doesn't get allocated at all.

提交回复
热议问题