Может кто-нибудь, пожалуйста, помогите мне в том, что я делаю неправильно? Всегда вызывается указатель базового класса! Я пытаюсь создать карту пользовательских объектов класса. Пробовал и поиск и индексирование напрямую, но тот же результат!Виртуальная функция, почему здесь называется функция базового класса?
#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