C++ member function pointer

//1.derived class can call the base class function, but the base class can not call the derived class function, member pointer can not do it
//2.c++ member function is strict
//3.the size of C++ pointer maybe not equal to the size of machine word

#include <stdio.h>


#include <iostream>
using namespace std;

//1.0--------------------------------------------------------------------------------------
//1.derived class can call the base class function, but the base class can not call the derived class function, member pointer can not do it

//2.c++ member function is  strict
class base
{
  int a;
public:
  void hello()
  {
    cout<<"base hello\n";
  }  

  void world()
  {
    cout<<"base world\n";
  }  
};

class Derived1: public base
{
public:
  void hello()
  {
    cout<<"Derived1 hello\n";
  }


  void call(Derived1 *b1, void (Derived1::*fun)())
  {
    (b1->*fun)();
  }

};

class Derived2:public base
{
public:
  void world()
  {
    cout<<"Derived2 world\n";
  }
};

/*
void call(base *b1, void (base::*fun)()) //can not work
{
  (b1->*fun)();
}
*/

void call(Derived1 *b1, void (Derived1::*fun)())
{
  (b1->*fun)();
}


typedef void (Derived1::*pFun)(); 
//1.1--------------------------------------------------------------------------------------

class BaseClass {
 public: 
     void BaseClassFun(int x, char *p) {
       printf("In BaseClass\n"); };
};

class DerivedClass : public BaseClass {
 public:
};

//1.1--------------------------------------------------------------------------------------



class CBase1 {};
class CBase2 {};
class CDerive2 : public CBase1, public CBase2 {};
typedef void (CDerive2::*FPderive2)();






int main() {
    // Declare a member function pointer for BaseClass

    typedef void (BaseClass::*m_f_p)(int, char *);
    m_f_p memfunc_ptr;
    memfunc_ptr = &DerivedClass::BaseClassFun; 

    DerivedClass *dc;
    char pstr[]="";
    ( dc->*memfunc_ptr)(1,pstr);
//--------------------------------------------------------
//3.the size of C++ pointer maybe not equal to the size of machine word     
    void (*pf)();
    cout << "sizeDerive2 = " << sizeof(pf) << endl;   
    size_t sizeDerive2 = sizeof(FPderive2);
    cout << "sizeDerive2 = " << sizeDerive2 << endl;
//---------------------------------------------------------

//  typedef void (base::*pFun)(); 
  Derived1 d1;
  Derived2 d2;  
  pFun pf1;
  pf1=&Derived1::hello;
  (&d1)->call(&d1,&Derived1::hello);

 // (&d1)->call(&d1,&Derived1::world);


//  (&d1)->call(&d1,&base::hello);

//  (&d1)->call(&d1,&base::world);  

  call(&d1,&base::world);  


  call(&d1,&Derived1::hello);

  call(&d1,&Derived1::world);

  call(&d1,&base::hello);

  call(&d1,&base::world);    



  return 0;

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值