C++语法小记---一个有趣的现象

下面的代码会飞吗?

 1 #include <iostream>
 2 #include <string>
 3 
 4 using namespace std;
 5 
 6 class Test
 7 {
 8 public:
 9     void show()
10     {
11         cout<<"show()"<<endl;
12     }
13 };
14 
15 int main()
16 {
17     Test *pt = NULL;
18     pt->show();  //会飞吗?
19     return 0;
20 }
  • 下面代码会跑飞吗?----不会

  • 原因:调用成员函数时,对象地址是作为参数传递的,只要在成员函数里面不调用就不会跑飞,但是this指针为NULL,调用成员函数本身不会涉及到对象地址

原文地址:https://www.cnblogs.com/chusiyong/p/11315375.html