#include <iostream>
/*
定义成员变量的指针
*/
class Cusor {
public:
Cusor() : a(99){};
static int Cusor::*data() { return &Cusor::a; }
private:
int a;
};
int Cusor::*pData = Cusor::data();
/*
定义成员函数的指针
*/
class Screen {
public:
using MyFunc = void (Screen::*)();
Screen() {}
~Screen() {}
enum Direct { HOME, FORWARD, REAR, LEFT, RIGHT };
void move(Direct dt) { (this->*mFuncs[dt])(); }
private:
void home() { std::cout << "home" << std::endl; }
void forward() { std::cout << "forward" << std::endl; }
void rear() { std::cout << "rear" << std::endl; }
void left() { std::cout << "left" << std::endl; }
void right() { std::cout << "right" << std::endl; }
private:
static MyFunc mFuncs[5];
};
Screen::MyFunc Screen::mFuncs[5] = {&Screen::home, &Screen::forward, &Screen::rear, &Screen::left,
&Screen::right};
int main() {
Cusor csr;
std::cout << "csr.*pData = " << csr.*pData << std::endl;
csr.*pData = 100;
std::cout << "csr.*pData = " << csr.*pData << std::endl;
Screen sc;
sc.move(Screen::HOME);
getchar();
return 0;
}