模拟类可以从googlemock中的另一个模拟类继承吗? [英] Can a mock class inherit from another mock class in googlemock?

查看:85
本文介绍了模拟类可以从googlemock中的另一个模拟类继承吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

模拟类可以从googlemock中的另一个模拟类继承吗?如果是,请帮助我理解为什么这不工作。

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));
};

我得到一个无法实例化抽象类错误为 MockB ,但不是 MockA

I get a cannot instantiate abstract class compiler error for MockB but not for MockA

推荐答案

如果您计划使用多重继承,则应使用虚拟继承。

If you plan on using multiple inheritance, you should be using virtual inheritance.

下一个示例编译并链接罚款:

Next example compiles and link fine :

class IA
{
    public:
        virtual int test1(int a) = 0;
};

class IB : virtual public IA
{
    public:
        virtual float test2(float b) = 0;
};

class MockA :virtual public IA
{
    public:
        int test1(int a)
        {
            return a+1;
        }
};

class MockB : public MockA, public IB
{
    public:
        float test2(float b)
        {
            return b+0.1;
        }
};

int main()
{
    MockB b;
    (void)b;
}

这只是您的例子的一个小修改

It is just a small modification of your example

这篇关于模拟类可以从googlemock中的另一个模拟类继承吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆