zoukankan      html  css  js  c++  java
  • Point to class member

    #include <iostream>
    using namespace std;
    
    class Student
    {
       public:
       Student(string n, int nu):name(n),num(nu){}
       string name;
       int num;
    };
    
    int main()
    {
       Student s("zhangsi", 100);
       Student *ps = &s;
    
       Student ss("zhaoqi", 100);
       Student *pss = &ss;
    //string *ps = &s.name;//the action destroy the encapsulation
    //下面讲的指针,是指向类层面的指针,而不是对象层面
    //要想使用,还要跟具体的对象产生关系
       string Student:: *psn = &Student::name;
       cout << s.*psn << endl;
       cout << ps->*psn << endl;
    
       cout << ss.*psn << endl;
       cout << pss->*psn << endl;
    
       return 0;
    }

    在c++中

    .*:Pointer to member

    ->*:Pointer to member

    Point to class function

    #include <iostream>
    using namespace std;
    
    class Student
    {
       public:
       Student(string n, int nu):name(n),num(nu){}
       void dis(int idx)
       {
           cout << "idx:" << idx << "name:" << name << "number:" << num << endl;
       }
       string name;
       int num;
    };
    
    int main()
    {
        void (Student::*pdis)(int idx) = &Student::dis;
        Student s("zhangsan", 100);
        Student *ps = &s;
        Student ss("zhangsan", 100);
        Student *pss = &ss;
        (s.*pdis)(1);
        (ss.*pdis)(2);
        (ps->*pdis)(1);
        (ps->*pdis)(2);
    
        return 0;
    }

    test1:
    #include <iostream>
    using namespace std;

    struct Point
    {
        int add(int x, int y)
        {
            return x + y;
        }
        int minus(int x, int y)
        {
            return x - y;
        }
        int multi(int x, int y)
        {
            return x * y;
        }
        int div(int x, int y)
        {
            return x / y;
        }
    };

    int oper(Point &p, int (Point::*pf)(int x, int y), int x, int y)
    {
        return (p.*pf)(x,y);
    }
    typedef int (Point::*PF)(int x, int y);
    int main()
    {
        Point p;
        PF pf = &Point::add;
        cout << oper(p, pf, 1, 2);
        return 0;
    }

     更加隐蔽的接口

    #include <iostream>
    using namespace std;
    class Game
    {
       public:
          Game()
          {
             pf[0] = &Game::f;
             pf[1] = &Game::g;
             pf[2] = &Game::h;
             pf[3] = &Game::l;
          }
         void select(int i)
         {
            if (i >= 0 && i <= 3)
            {
               (this->*pf[i])(i);
            }
         }
       private:
         void f(int idx) { cout << "void f(int idx)" << "idx:" << idx << endl; }
         void g(int idx) { cout << "void g(int idx)" << endl; }
         void h(int idx) { cout << "void h(int idx)" << endl; }
         void l(int idx) { cout << "void l(int idx)" << endl; }
         enum{
            nc = 4
         };
    
         void (Game::*pf[nc])(int idx);  
    };
    
    int main()
    {
       Game g;
       g.select(1);
       g.select(2);
       g.select(3);
       return 0;
    }
  • 相关阅读:
    Win10中的IIS10安装php manager和IIS URL Rewrite
    第十四周
    第十三周
    第十二周
    第十一周
    第十周
    第九周
    测试作业
    第八周
    第七周
  • 原文地址:https://www.cnblogs.com/aelite/p/10810371.html
Copyright © 2011-2022 走看看