⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 arrayptrfunction.cpp

📁 24学时攻克C++光盘源代码 深入浅出 方便实用
💻 CPP
字号:
 //Listing 20.10 Array of pointers to member functions
 #include <iostream>

 class Dog
 {
 public:
     void Speak()const { std::cout << "Woof!\n"; }
     void Move() const { std::cout << "Walking to heel...\n"; }
     void Eat() const { std::cout << "Gobbling food...\n"; }
     void Growl() const { std::cout << "Grrrrr\n"; }
     void Whimper() const { std::cout << "Whining noises...\n"; }
     void RollOver() const { std::cout << "Rolling over...\n"; }
     void PlayDead() const
         { std::cout << "Is this the end of Little Caesar?\n"; }
 };

 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)
     {
         std::cout << "(0)Quit (1)Speak (2)Move (3)Eat (4)Growl";
         std::cout << " (5)Whimper (6)Roll Over (7)Play Dead: ";
         std::cin >> Method;
         if (Method == 0)
         {
             fQuit = true;
             break;
         }
         else
         {
             pDog = new Dog;
             (pDog->*DogFunctions[Method-1])();
             delete pDog;
         }
     }
     return 0;
 }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -