C++ 必知必会:条款16 指向成员函数的指针并非指针

这一点与指向成员的指针类似,其实现可能更加复杂,因为成员函数同时还存在虚拟函数,需要动态绑定执行动作。当然这种属性是属于函数本身的,此处表达的是指针不涉及函数的属性问题。

   1:  class shape
   2:  {
   3:  public:
   4:      // void moveTo(point newLocation);
   5:      bool validate();
   6:      //…
   7:  }
   8:   
   9:  class circle:: public shape
  10:  {
  11:  public:
  12:      //…
  13:      bool draw() const;
  14:      //…

指向成员函数的指针

   1:  void (shape:: *mf1)(point) = &shape:moveTo; //
   2:  bool (circle:: *mf2)() const = &circle::draw; //与普通函数不同,成员函数的指针可以指向常量成员函数

指向成员函数的指针与指向类成员的指针类似,存在指向基类的成员函数指针向指向子类的成员函数指针的自定义转换。

   1:  circle  *pCircle = new circle;
   2:  pCircle->*mf1(somepoint) // 这种调用时合法的
   3:   
   4:  shape *pShape = new shape;
   5:  pShape->draw();// error  shape根本就没有draw成员函数,如何调用啊?
原文地址:https://www.cnblogs.com/hwtxf/p/3515391.html