#include "stdafx.h" #include <iostream> #include <typeinfo> using namespace std; class A { public: virtual void foo() { cout<<"A foo"<<endl; } void pp() { cout<<"A PP"<<endl; } }; class B:public A { public: B() { num=100; } void foo() { cout<<"B foo"<<endl; } void pp() { cout<<"B PP"<<endl; } void FunctionB() { cout<<"Excute FunctionB!"<<endl; } void show() { cout<<num; } private: int num; }; int main(int argc,char* argv[]) { A a; A* pa=&a; B *pb0,*pb; pb0->FunctionB(); pb=dynamic_cast<B*>(pa); if(pb==NULL) { cout<<"The pointer pb is null"<<endl; } (dynamic_cast<B*>(pa))->FunctionB(); (dynamic_cast<B*>(pa))->foo(); //执行出错,因为需要this指针 (dynamic_cast<B*>(pa))->show(); //执行出错,因为需要this指针 system("pause"); return 0; }
C++调用成员函数需要this指针的情况,布布扣,bubuko.com
原文地址:http://blog.csdn.net/cjc211322/article/details/38445485