누군가 내가 잘못하고있는 것을 도와 줄 수 있습니까? 항상 기본 클래스 포인터가 호출됩니다! 나는 사용자 정의 클래스 객체의 Map을 만들기 위해 노력하고있다. find와 indexing을 통해 직접 시도해 보았지만 같은 결과를 얻었습니다!가상 함수, 기본 클래스 함수가 여기에서 호출되는 이유는 무엇입니까?
#include "stdafx.h"
#include <iostream>
#include <string>
#include <Map>
#include <algorithm>
class Command
{
public:
virtual int execute(std::string *args) { std::cout << "Base called ! ERROR!\n"; return -1; }
};
class ShowNames : public Command
{
public:
int execute(std::string names)
{
std::cout << names;
return 0;
}
};
class ShowNos : public Command
{
public:
int execute(std::string Nos)
{
std::cout << Nos;
return 0;
}
};
typedef std::map<std::string, Command*> CmdList;
CmdList buildMaps()
{
CmdList c1;
ShowNames s1;
ShowNos n1;
c1["names"] = new ShowNames();
c1["nos"] = new ShowNos();
//c1.find("names")
return c1;
}
void testCommandList()
{
CmdList commands;
Command *c1;
commands = buildMaps();
std::string cmd,args;
std::cout << "Enter your command: ";
std::cin >> cmd;
std::cout << "Enter args for the command: ";
std::cin >> args;
auto it = commands.find(cmd);
if (it != commands.end())
{
it->second->execute(&args);
}
else
{
std::cout << "Command not found, try again\n";
}
}
정보를 알려 주셔서 감사합니다. – vinit