zoukankan      html  css  js  c++  java
  • 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成员函数,如何调用啊?
  • 相关阅读:
    Javascript操作cookie
    上传文件-layui+ashx
    Mysql对查询结果添加序列号
    Maven详解
    解决 go iris ReadJSON 无法获取 json,并提示 unexpected end of JSON input 的错误
    Archlinux 最新安装方法 (2020.07.01-x86_64)之虚拟机 BIOS 安装
    Oracle APEX 发送邮件
    包管理Go module的使用
    Golang 解决 Iris 被墙的依赖包
    解决 Nginx 代理Apex慢的问题
  • 原文地址:https://www.cnblogs.com/hwtxf/p/3515391.html
Copyright © 2011-2022 走看看