函数指针调用

来源:互联网 发布:js面向对象 继承例子 编辑:程序博客网 时间:2024/06/05 20:52
#include  <iostream>
using namespace std;
class Base
{
  public :
    virtual void Print() { cout<<"This is Base"<<endl;}
};
   // void PrintB() { cout<<"This is base printb"<<endl;}
class Base2
{
  public:
    virtual void Print2() {cout <<"This is base2"<<endl;}
};
class Derived: public Base,public Base2
{
  public:
    void Print() {cout<<"This is Derived"<<endl;}
    void Print2() {cout<<"This is Derived2"<<endl;}
    void Out() {cout<<"This should not be called"<<endl;}
};
typedef void (*Func)();
int main()
{
  Base *pb = new Derived();

 // Derived *db = new Derived();

  pb->Print();

//基类不能调用基类中没有virutal的子类函数

 //pb->Out();

  //db->PrintB();

  int* pA = (int*)(*((int*)pb+1));

//将地址强制转换成函数指针

  Func pFunc = (Func)*pA;

//调用地址即调用函数指针

  pFunc();
  return 0;
}
0 0
原创粉丝点击