이전 답변은 해당 솔루션에 대한 것이지만 솔루션은 문제에 대해 올바른 것으로 보이지 않습니다.
두 가지 제안이 있습니다. 1) 더 나은 추상 기본 클래스를 작성하거나 2) 펑터에 기본 클래스를 사용하십시오. (함수 객체).프로세서의 명령어 세트를 시뮬레이션 프로젝트 감안할 때
, 우리는 함께 실행 시뮬레이션 할 수 있습니다 ... 더 자세히들을 살펴 보자 :
이
struct Instruction_Interface
{
virtual void execute(void) = 0;
};
typedef std::vector<Instruction_Interface *> Instruction_Container;
//...
Instruction_Container::iterator iter;
Instruction_Container program_instructions;
//...
for (iter = program_instructions.begin();
iter != program_instructions.end();
++iter)
{
(*iter)->execute(); // Execute the instruction;
}
이 각 명령의 execute
방법의 호출을 할 수 있습니다를,에 관계없이의 종류의 지시. 인터페이스에 메소드를 더 추가 할 수 있습니다. 이 예에서는 명령을 텍스트로 변환하는 "toString"을 추가 할 수 있습니다.
Idea 2: Functors
사용할 기능에 대한 기본 클래스 또는 인터페이스를 설정하십시오. 그것들을 컨테이너에 넣고 컨테이너를 반복합니다. 반복은 조건도 될 수 있습니다 요약
struct Functor_Interface
{
virtual std::string get_name(void) const = 0;
virtual void execute(void) = 0;
virtual void execute_if_name(const std::string& name)
{ if (name == get_name())
{
execute();
}
}
};
typedef std::vector<Functor_Interface *> Functor_Container;
//...
Functor_Container the_functors;
//...
Functor_Container::iterator iter;
for (iter = the_functors.begin();
iter != the_functors.end();
++iter)
{
(*iter)->execute_if_name("loader"); // Execute the functor if it is a *loader*.
(*iter)->execute_if_name("math");
}
는,이 함수 이름을 필요로하지 않는 좋은 방법이지만, 대신 중 함수가 실행을 결정할 수 있습니다 또는 일반적으로 블라인드 실행 있는지 확인하기 위해 설계를 통해 생각 행동 양식.
당신이 달성하려고하는 무엇을? –