Accessing class members on a NULL pointer

后端 未结 8 1615
孤城傲影
孤城傲影 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:06

    Dereferencing a NULL pointer causes "undefined behaviour", This means that anything could happen - your code may even appear to work correctly. You must not depend on this however - if you run the same code on a different platform (or even possibly on the same platform) it will probably crash.

    In your code there is no Foo object, only a pointer which is initalised with the value NULL.

提交回复
热议问题