main.cpp

来自「C++ Source code from a tutorial」· C++ 代码 · 共 71 行

CPP
71
字号
#include <iostream>
#include <stdlib.h>
#include "command.h"

using namespace std;

// These are my commands
class MyCommands {
public:
    void MenuNew() { cout << "New!" << endl; }
    void MenuOpen() { cout << "Open!" << endl; }
    void MenuClose() { cout << "Close!" << endl; }
    void MenuQuit() { cout << "Quit!" << endl; }
};

// These are more commands
class MoreCommands {
public:
    void CrashComputer() { cout << "Oops!" << endl; }
    void ForgetEverything() { cout << "I forgot!" << endl; }
};

// These are the handlers that call my commands
Command *NewCommand;
void NewMenuHandler() {
    NewCommand->Execute();
}

Command *OpenCommand;
void OpenMenuHandler() {
    OpenCommand->Execute();
}

int main(int argc, char *argv[])
{
    MyCommands *commands = new MyCommands();
    NewCommand = 
        new SpecificCommand<MyCommands>
        (commands, &MyCommands::MenuNew);
    OpenCommand = 
        new SpecificCommand<MyCommands>
        (commands, &MyCommands::MenuOpen);

    // Test out the menu items by
    // calling the handlers directly.
    NewMenuHandler();
    OpenMenuHandler();
    
    // Now change my mind. I want the handlers
    // to work with a different class.
    delete NewCommand; 
    delete OpenCommand;
    MoreCommands *morecommands = new MoreCommands();
    NewCommand = 
        new SpecificCommand<MoreCommands>
        (morecommands, &MoreCommands::CrashComputer);
    OpenCommand = 
        new SpecificCommand<MoreCommands>
        (morecommands, &MoreCommands::ForgetEverything);

    // Test out the menu items by
    // calling the handlers directly.
    NewMenuHandler();
    OpenMenuHandler();

    system("PAUSE");	
    return 0;
}


⌨️ 快捷键说明

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