美文网首页
c++对象实例化问题

c++对象实例化问题

作者: rzzdy | 来源:发表于2018-01-31 13:47 被阅读5次
#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,但在编译阶段
    只会检测pAA类中的test_echo方法,静态绑定此方法,不像java,python在动态执行时才绑定NULL
  • 参考:http://m.nowcoder.com/questions?uuid=2c126b876d714f4eb3be3de7dbf700e9

相关文章

网友评论

      本文标题:c++对象实例化问题

      本文链接:https://www.haomeiwen.com/subject/nuikzxtx.html