C++ 使用接口和默认实现继承
C++ Inheritance with Interface and Default Implementation
我不确定该怎么称呼这种继承方案,但我正在尝试使用具有默认实现的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 是一个抽象类,这并不让我感到惊讶。有人知道我如何让这个默认实现想法起作用吗?关键是不必为每个实现手动编写克隆函数。这可能不可行,但我没有想法可以尝试。
您可以执行以下操作:
#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> { /*...*/ };
两个注意事项:
为了能够访问
ICloneable
的模板参数InterfaceType_
您需要将其设置为模板别名,然后使用using typename Base::InterfaceType
(因为它是模板参数依赖类型(。我已经为
CloneableMixin
Base
模板参数提供了默认类型 - 这允许将其用于您希望实现clone
基类。
此外,两个不相关的评论:
您不需要键入
virtual
- 这是隐含的。最好在末尾添加override
(这样可以确保该方法实际覆盖某些内容,否则编译器将报告错误(。您可以考虑使用
std::make_shared
而不是new
。
相关文章:
- 为什么大多数 pair 实现默认不使用压缩(空基优化)?
- 默认赋值运算符如何在实际 STL 中实现
- C++ 使用接口和默认实现继承
- 堆栈和队列的C++默认实现
- 为模板类中的纯虚函数提供默认实现
- C++头/实现文件中的默认和重载构造函数?
- C++:默认构造函数实现
- 部分模板函数专用化enable_if:默认实现
- 有没有一种很好的方法来实现具有默认失败情况的条件类型?
- 如何在接口的默认实现中使用类
- 为什么我不能使私人运营商成为新的并使用默认实现?
- 没有虚拟调度的默认方法实现
- 是已定义的流的实现的默认模式
- 在类实现中为字符串的覆盖默认破坏者
- 派生类中虚拟方法的默认实现
- 是否可以在没有预处理器宏的情况下为"virtual" get-setter 定义默认实现
- 我是否可以将复制构造函数设为私有而仍然使用默认实现?
- 如何为可重写的方法提供默认实现?
- 当在全局命名空间中覆盖new/delete时,我应该使用std-rtl中的new/delete的默认实现
- 具有默认实现的抽象工厂设计模式