list1511.cpp
来自「teach yourself C++ in 21 days 第五版」· C++ 代码 · 共 54 行
CPP
54 行
//Listing 15.11 Array of pointers to member functions
#include <iostream>
using std::cout;
using std::endl;
class Dog
{
public:
void Speak()const { cout << "Woof!" << endl; }
void Move() const { cout << "Walking to heel..." << endl; }
void Eat() const { cout << "Gobbling food..." << endl; }
void Growl() const { cout << "Grrrrr" << endl; }
void Whimper() const { cout << "Whining noises..." << endl; }
void RollOver() const { cout << "Rolling over..." << endl; }
void PlayDead() const
{ cout << "The end of Little Caesar?" << endl; }
};
typedef void (Dog::*PDF)()const ;
int main()
{
const int MaxFuncs = 7;
PDF DogFunctions[MaxFuncs] =
{Dog::Speak,
Dog::Move,
Dog::Eat,
Dog::Growl,
Dog::Whimper,
Dog::RollOver,
Dog::PlayDead };
Dog* pDog =0;
int Method;
bool fQuit = false;
while (!fQuit)
{
cout << "(0)Quit (1)Speak (2)Move (3)Eat (4)Growl";
cout << " (5)Whimper (6)Roll Over (7)Play Dead: ";
std::cin >> Method;
if (Method <= 0 || Method >= 8)
{
fQuit = true;
}
else
{
pDog = new Dog;
(pDog->*DogFunctions[Method-1])();
delete pDog;
}
}
return 0;
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?