#include <iostream>
#include <stdio.h>
using namespace std;
class A {
public:
void test_echo() {
printf("%d", 123);
}
};
int main() {
cout << "test\n";
A *pA = NULL;
pA->test_echo();
return 0;
}
以上代码执行后,会正常输出123,因为:
- 此处
printf的输出没有用到this,所以不会引起segment fault. - c++在编译时,除了虚函数外,会将一切可编译处理的都编译掉,虽然
pA被赋值了NULL,但在编译阶段
只会检测pA的A类中的test_echo方法,静态绑定此方法,不像java,python在动态执行时才绑定NULL。 - 参考:http://m.nowcoder.com/questions?uuid=2c126b876d714f4eb3be3de7dbf700e9