#include <iostream>#include <vector>class BaseCommand { public: enum CommandType { Unknown, TypeA, TypeB }; BaseCommand() = default; virtual void execute() = 0; virtual BaseCommand::CommandType commandType() = 0;};class CommandA : public BaseCommand { public: CommandA() = default; void execute() override { std::cout << "execure CommandA" << std::endl; } BaseCommand::CommandType commandType() override { return BaseCommand::TypeA; }};class CommandB : public BaseCommand { public: CommandB() = default; void execute() override { std::cout << "execure CommandB" << std::endl; } BaseCommand::CommandType commandType() override { return BaseCommand::TypeB; }};template <class>struct CommandProperty;template<>struct CommandProperty<CommandA>{ static const bool is_single = false; static const BaseCommand::CommandType m_type = BaseCommand::TypeA;};template<>struct CommandProperty<CommandB>{ static const bool is_single = true; static const BaseCommand::CommandType m_type = BaseCommand::TypeB;};std::vector<BaseCommand *> m_arr;template<class Command>bool checkCommand(){ bool is_single = CommandProperty<Command>::is_single; if (is_single) { for (std::vector<BaseCommand *>::iterator it = m_arr.begin() ; it != m_arr.end(); ++it) if ((*it)->commandType() == CommandProperty<Command>::m_type) { return false; } } return true;}int main(int argc, char *argv[]){ if (checkCommand<CommandA>()) m_arr.push_back(new CommandA); if (checkCommand<CommandA>()) m_arr.push_back(new CommandA); if (checkCommand<CommandB>()) m_arr.push_back(new CommandB); if (checkCommand<CommandB>()) m_arr.push_back(new CommandB); for (std::vector<BaseCommand *>::iterator it = m_arr.begin() ; it != m_arr.end(); ++it) (*it)->execute(); return 0;}
#include <iostream>#include <vector>enum CommandType { Unknown = 0, TypeA = 1, TypeB = 2};class ICommand {public: virtual void execute() = 0; virtual CommandType commandType() = 0;};template<bool isSingle, CommandType type>class BaseCommand : public ICommand { public: BaseCommand() = default; static bool is_single() {return isSingle;} static CommandType CommandTypeB(){return type;}};class CommandA : public BaseCommand<false, CommandType::TypeA> { public: CommandA() = default; void execute() override { std::cout << "execure CommandA" << std::endl; } CommandType commandType() override { return CommandTypeB(); }};class CommandB : public BaseCommand<true, CommandType::TypeB> { public: CommandB() = default; void execute() override { std::cout << "execure CommandB" << std::endl; } CommandType commandType() override { return CommandTypeB(); }};std::vector<ICommand *> m_arr;template<class Command>bool checkCommand(){ bool is_single = Command::is_single(); if (is_single) { for (std::vector<ICommand *>::iterator it = m_arr.begin() ; it != m_arr.end(); ++it) if (((ICommand*)(*it))->commandType() == Command::CommandTypeB()) { return false; } } return true;}int main(int argc, char *argv[]){ if (checkCommand<CommandA>()) m_arr.push_back(new CommandA); if (checkCommand<CommandA>()) m_arr.push_back(new CommandA); if (checkCommand<CommandB>()) m_arr.push_back(new CommandB); if (checkCommand<CommandB>()) m_arr.push_back(new CommandB); for (std::vector<ICommand *>::iterator it = m_arr.begin() ; it != m_arr.end(); ++it) (*it)->execute(); return 0;}
C++ (Qt) for (std::vector<ICommand *>::iterator it = m_arr.begin() ; it != m_arr.end(); ++it) (*it)->execute();
C++ (Qt) for (ICommand * command : m_arr) command->execute();
for (auto command : m_arr) command->execute();
C++ (Qt)CommandA * ca = new CommandA;if (ca->IsValid()) ...
C++ (Qt) CommandA *ca = new CommandA; if (checkCommand(ca)) { ca->execute(); }else { delete ca;}
for (auto & command : m_arr) command->execute();
C++ (Qt)template<class Command>bool checkCommand(){...}