C++调用成员函数需要this指针的情况

2015-07-20 17:57:52 · 作者: · 浏览: 4

1、虚成员函数,因为需要this指针寻找虚表指针


2、成员函数中对数据成员进行了操作

#include "stdafx.h"
#include 
  
   
#include 
   
     using namespace std; class A { public: virtual void foo() { cout<<"A foo"<
    
     FunctionB(); pb=dynamic_cast
     
      (pa); if(pb==NULL) { cout<<"The pointer pb is null"<
      
       (pa))->FunctionB(); (dynamic_cast
       
        (pa))->foo(); //执行出错,因为需要this指针 (dynamic_cast
        
         (pa))->show(); //执行出错,因为需要this指针 system("pause"); return 0; }