这是我的问题:我在 .h 文件中定义了一个虚拟方法,我想在从基类继承的类中调用它。可悲的是,派生类中的方法没有被调用。有没有更好的方法来实现我正在尝试做的事情?
#ifndef ofxBASE_SND_OBJ
#define ofxBASE_SND_OBJ
#include "ofConstants.h"
class ofxBaseSndObj {
public:
virtual string getType(){}
string key;
};
#endif
这是我的嗡嗡声课
#ifndef OFXSO_BUZZ
#define OFXSO_BUZZ
#include "ofxBaseSndObj.h"
class ofxSOBuzz : public ofxBaseSndObj
{
public:
string getType();
};
#endif
ofxSOBuzz.cpp
string ofxSOBuzz::getType()
{
string s = string("ofxSOBuzz");
printf(" ********* returning string type %s", s.c_str()); // doesn't get called!
return s;
}
然后在另一个类中,我尝试这样称呼它:
string ofxSndObj::createFilter(ofxBaseSndObj obj)
{
string str = obj.getType();
if(str.compare("ofxSOBuzz") == 0)
{
printf(" all is well ");
}
}
在上面的方法中,我需要能够传入多种对象中的一种,这些对象都扩展了 ofxBaseSndObj 对象。任何建议或指示将不胜感激。谢谢!