模拟类可以从googlemock中的另一个模拟类继承吗?
模拟类可以从googlemock中的另一个模拟类继承吗?如果是,请帮助我理解为什么这不起作用。
class IA
{
public:
virtual int test1(int a) = 0;
};
class IB : public IA
{
public:
virtual float test2(float b) = 0;
};
class MockA : public IA
{
public:
MOCK_METHOD1(test1, int (int a));
};
class MockB : public MockA, public IB
{
public:
MOCK_METHOD1(test2, float (float b));
};
我收到 MockB
的 cannot instantiate Abstract class
编译器错误,但 MockA
没有收到错误
Can a mock class inherit from another mock class in googlemock? If yes, then please help me in understanding why isn't this working.
class IA
{
public:
virtual int test1(int a) = 0;
};
class IB : public IA
{
public:
virtual float test2(float b) = 0;
};
class MockA : public IA
{
public:
MOCK_METHOD1(test1, int (int a));
};
class MockB : public MockA, public IB
{
public:
MOCK_METHOD1(test2, float (float b));
};
I get a cannot instantiate abstract class
compiler error for MockB
but not for MockA
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您计划使用多重继承,则应该使用虚拟继承。
下一个示例编译并链接良好:
这只是您的示例的一个小修改
If you plan on using multiple inheritance, you should be using virtual inheritance.
Next example compiles and link fine :
It is just a small modification of your example
MockB
类继承自IB
,它有两个纯抽象函数:test1
和test2
。并且您需要覆盖它们。继承覆盖test1
的MockA
是不够的(至少在 C++ 中 - 在 Java 中它可以工作)。因此解决方法是添加到
MockB
定义中。Class
MockB
inherits fromIB
which has two purely abstract functions:test1
andtest2
. And you need to override both of them. Inheriting fromMockA
which overridestest1
is insufficient (at lest in C++ - in Java it would work). So the fix is to addto
MockB
definition.