在 C # 中开发了一段时间之后,我注意到如果你声明一个抽象类用作接口,你不能实例化这个抽象类的一个向量来存储子类的实例。
#pragma once
#include <iostream>
#include <vector>
using namespace std;
class IFunnyInterface
{
public:
virtual void IamFunny() = 0;
};
class FunnyImpl: IFunnyInterface
{
public:
virtual void IamFunny()
{
cout << "<INSERT JOKE HERE>";
}
};
class FunnyContainer
{
private:
std::vector <IFunnyInterface> funnyItems;
};
在 MS VS2005中声明向量抽象类的行导致了这个错误:
error C2259: 'IFunnyInterface' : cannot instantiate abstract class
我看到了一个显而易见的解决方案,即用以下代码替换 IFunnyInterface:
class IFunnyInterface
{
public:
virtual void IamFunny()
{
throw new std::exception("not implemented");
}
};
这是一个可以接受的 C + + 变通方案吗? 如果没有,是否有任何第三方图书馆像升级,可以帮助我解决这个问题?
谢谢你读这个!
安东尼