小编Clu*_*ess的帖子

用C++实现接口

我通常使用C#编程,但我正在尝试做一些C++,并且在尝试用C++实现接口方面有点挣扎.

在C#中我会做这样的事情:

class Base<T>
{
    public void DoSomething(T value)
    {
        // Do something here
    }
}

interface IDoubleDoSomething
{
    void DoSomething(double value);
}

class Foo : Base<double>, IDoubleDoSomething
{
}
Run Code Online (Sandbox Code Playgroud)

在C++中我实现了这样:

template <class T>
class Base
{
public:
    virtual void DoSomething(T value)
    {
        // Do something here
    }
};

class IDoubleDoSomething
{
public:
    virtual void DoSomething(double value) = 0;
};

class Foo : public Base<double>, public IDoubleDoSomething
{
};
Run Code Online (Sandbox Code Playgroud)

问题是我无法实例化Foo,因为它是抽象的(不实现DoSomething).我意识到我可以实现DoSomething并在Base上调用该方法,但我希望有更好的方法来做到这一点.我有其他类从base继承不同的数据类型,我有其他类继承自不使用Base的IDoubleDoSomething.

任何帮助赞赏.

c# c++ porting interface multiple-inheritance

9
推荐指数
1
解决办法
5981
查看次数

标签 统计

c# ×1

c++ ×1

interface ×1

multiple-inheritance ×1

porting ×1