带接口和默认实现的C ++继承

问题描述 投票:2回答:1

我不确定该怎么称为继承方案,但我正在尝试使用带有默认实现的cloneable接口。我有一些问题,但正在获得正确的方案。

我基于C#中定义的可克隆接口。

首先,我有我的界面和默认实现:

template<class BaseType>
class ICloneable
{
public:
    virtual std::shared_ptr<BaseType> Clone() const = 0;
};

template<class BaseType, class DerivedType>
class Cloneable : public ICloneable<BaseType>
{
public:
    virtual std::shared_ptr<BaseType> Clone() const
    {
        return std::shared_ptr<BaseType>(new DerivedType(*(DerivedType*)this));
    }
};

我的愿望是有以下方案。

// A pure virtual base interface
class Base : public ICloneable<Base>
{
public:
    virtual void SomeFunc() = 0;
}

// Another implementation
class Imp1 : public Base, Cloneable<Base, Imp1>
{
    public:
        virtual void SomeFunc() {}
}
// An implementation
class Imp2 : public Cloneable<Base, Imp2>
{
    public:
        virtual void SomeFunc() {}
}

如果我有一个`std :: shared_ptr'对象的列表,我可以在我想要进行深度复制时调用Clone函数,而不必在每个实现中手动编写该函数。

现在我得到的Imp是一个抽象类,这并不让我感到惊讶。任何人都知道如何让这个默认的实现想法工作?关键是不必为每个实现手动编写克隆功能。它可能不可行,但我没有想法尝试。

c++ inheritance virtual-functions
1个回答
2
投票

您可以执行以下操作:

#include <memory>

template<typename InterfaceType_>
struct ICloneable
{
    using InterfaceType = InterfaceType_;

    virtual ~ICloneable() = default;

    virtual std::shared_ptr<InterfaceType> clone() const = 0;
};

template<typename T, typename Base = ICloneable<T>>
struct CloneableMixin : public Base
{
    using InterfaceType = typename Base::InterfaceType;

    // With the following line uncommented, code does not compile in MSVC
    //using typename Base::InterfaceType;

    std::shared_ptr<InterfaceType> clone() const override
    { return std::make_shared<T>(*static_cast<const T*>(this)); }
};

现在,这可以使用如下:

struct SomeBaseClass : public CloneableMixin<SomeBaseClass> { /*...*/ };

struct SomeDerivedClass : public CloneableMixin<SomeDerivedClass, SomeBaseClass> { /*...*/ };

两个笔记:

  • 为了能够访问InterfaceType_ICloneable模板参数,您需要使其成为模板别名,然后使用using typename Base::InterfaceType(因为它是模板参数依赖类型)。
  • 我为BaseCloneableMixin模板参数提供了默认类型 - 这允许将它用于基类,您希望为其实现clone

此外,两个无关的评论:

  • 你不需要输入virtual - 这是隐含的。最后添加override是个好主意(这可以确保该方法实际上覆盖了某些内容,否则编译器将报告错误)。
  • 您可以考虑使用std::make_shared而不是new
© www.soinside.com 2019 - 2024. All rights reserved.