我被困住了,已经尝试了很多来解决这个“虚拟”问题,所以我请求你帮助我,因为这可能是一个“训练有素的眼睛”可以在几秒钟内解决的愚蠢问题。
问题:当我在 main 中执行以下操作时:
PrologConnector swiProlog;
swiProlog = PrologConnector::connectorFactory(PrologConnector::swi,argv);
swiProlog.send("blabla");
总是调用 PrologConnector 类的发送方法,而不是子类的发送方法。你看到问题了吗?
谢谢您的帮助!!
这是代码:PrologConnector.h
class PrologConnector {
virtual int send(char * cmd);
virtual int init(char **argv);
static PrologConnector connectorFactory(Prolog prolog, char ** argv);
};
PrologConnector.cpp
int PrologConnector::send(char * argv) {
std::cout << "not wanted"<<std::endl;
return 0;
}
int PrologConnector::init(char **argv) {
//TODO add implementation
return 0;
}
PrologConnector PrologConnector::connectorFactory(Prolog prolog, char **argv) {
if (prolog == swi) {
SWIConnector sc;
sc.init(argv);
return sc;
}
std::cout <<"Error in initialization!"<<std::endl;
PrologConnector pc;
return pc;
}
SWIConnector.h:
class SWIConnector : public PrologConnector {
int send(char *cmd);
int init(char **argv);
};
SWIConnector.cpp:
int SWIConnector::init(char **argv) {
//some action going on
}
int SWIConnector::send(char * cmd) {
//some action going on
}