Why am I able to make a function call using an invalid class pointer

后端 未结 6 606
没有蜡笔的小新
没有蜡笔的小新 2020-12-02 01:56

In below code snippet, although pointer is not initialized the call is still made successfully

temp *ptr;
ptr->func2();

Is it due to C++

6条回答
  •  眼角桃花
    2020-12-02 02:41

    I suspect that ptr->func2() is optimised away as it always returns true. But as Richie says, the use of the pointer is fine in this case as you;re not touching any instance data (a) and as such there is nothing to crash into.

提交回复
热议问题