c ++接口必须遵守五条规则吗?

问题描述 投票:12回答:3

在定义接口类时声明实例化方法的正确方法是什么?

出于显而易见的原因,抽象基类需要具有虚拟析构函数。但是,然后给出以下编译警告:“'InterfaceClass'定义了一个非默认的析构函数,但没有定义复制构造函数,复制赋值运算符,移动构造函数或移动赋值运算符”,这是“五个规则” ”。

我理解为什么一般应该遵守“五规则”,但是它仍适用于抽象基类或接口吗?

我的实现是:

class InterfaceClass
{
    //  == INSTANTIATION ==
  protected:
    //  -- Constructors --
    InterfaceClass()                      = default;
    InterfaceClass(const InterfaceClass&) = default;
    InterfaceClass(InterfaceClass&&)      = default;

  public:
    //  -- Destructors --
    virtual ~InterfaceClass() = 0;


    //  == OPERATORS ==
  protected:
    //  -- Assignment --
    InterfaceClass& operator=(const InterfaceClass&) = default;
    InterfaceClass& operator=(InterfaceClass&&)      = default;


    //  == METHODS ==
  public:
    // Some pure interface methods here...
};



//  == INSTANTIATION ==
//  -- Destructors --
InterfaceClass::~InterfaceClass()
{
}

它是否正确?这些方法应该是= delete吗?有没有办法声明析构函数是虚拟的纯粹,同时还有某种方式保持默认?

即使我将析构函数声明为:virtual ~InterfaceClass() = default;,如果我没有明确地默认其他四个,那么我将获得相同的编译器警告。

Tl; dr:满足接口类的“五规则”的正确方法是什么,因为用户必须定义​​虚拟析构函数。

感谢您的时间和帮助!

c++ abstract-class c++17 virtual-destructor rule-of-three
3个回答
3
投票

它是否正确?这些方法应该=删除吗?

你的代码似乎是对的。当您尝试以多态方式复制派生类时,需要将特殊的复制/移动成员函数定义为默认和受保护。考虑这个额外的代码:

#include <iostream>

class ImplementationClass : public InterfaceClass
{
  private:
    int data;
  public:
    ImplementationClass()
    {
        data=0;    
    };
    ImplementationClass(int p_data)
    {
        data=p_data;
    };
    void print()
    {
        std::cout<<data<<std::endl;
    };
};


int main()
{
    ImplementationClass A{1};
    ImplementationClass B{2};
    InterfaceClass *A_p = &A;
    InterfaceClass *B_p = &B;
    // polymorphic copy
    *B_p=*A_p;
    B.print();
    // regular copy
    B=A;
    B.print();
    return 0;
}

并考虑在InterfaceClass中定义特殊复制/移动成员函数的4个选项。

  1. 复制/移动成员函数=删除

通过在InterfaceClass中删除特殊的复制/移动成员函数,可以防止多态复制:

*B_p = *A_p; // would not compile, copy is deleted in InterfaceClass

这很好,因为多态副本也无法复制派生类中的数据成员。

另一方面,您也会阻止正常复制,因为如果没有基类复制赋值运算符,编译器将无法隐式生成复制赋值运算符:

B = A; //  would not compile either, copy assignment is deleted in ImplementationClass 
  1. 复制/移动特殊成员函数公开

使用复制/移动特殊成员函数作为默认和公共(或不定义复制/移动成员函数),正常复制将起作用:

B = A; //will copile and work correctly

但是多态复制将被启用并导致切片:

*B_p = *A_p; // will compile but not copy the extra data members in the derived class. 
  1. 复制/移动未定义的特殊成员函数

如果未定义移动和复制特殊成员函数,则与复制相关的行为类似于2:编译器将隐式生成已弃用的复制特殊成员(导致多态切片)。但是在这种情况下,编译器不会隐式生成移动特殊成员,因此可以在可以移动的地方使用复制。

  1. 受保护的复制/移动成员功能(您的提议)

使用特殊的复制/移动成员函数作为默认值并受保护,如在您的示例中,您将防止可能导致切片的多态复制:

*B_p = *A_p; // will not compile, copy is protected in InterfaceClass

但是,编译将显式生成默认的复制赋值运算符InterfaceClass,而ImplementationClass将能够隐式生成其复制赋值运算符:

B = A; //will compile and work correctly

所以你的方法似乎是最好和最安全的选择


1
投票

对于析构函数,如果要将其设置为纯虚拟和默认,则可以在实现中将其默认:

class InterfaceClass
{
    //  -- Destructors --
    virtual ~InterfaceClass() = 0;
};

InterfaceClass::~InterfaceClass() = default;

但是,如果析构函数是默认值或为空,则没有太大区别。

现在问你的其余部分。

通常,您应该具有复制构造函数和赋值运算符默认值。这样,它们不会阻止在派生类中创建默认赋值运算符和复制构造函数。默认实现是正确的,因为没有复制的不变量。

因此,如果您想轻松实现Clone方法,删除复制构造函数会有害:

class InterfaceClass
{
    virtual  InterfaceClass* Clone() = 0;
    virtual ~InterfaceClass() = 0;
};

class ImplementationClass : public InterfaceClass
{
public:
    // This will not work if base copy constructor is deleted
    ImplementationClass(const ImplementationClass&) = default; 
    // Writing copy constructor manually may be cumbersome and hard to maintain,
    // if class has a lot of members

    virtual  ImplementationClass* Clone() override
    {
        return new ImplementationClass(*this); // Calls copy constructor
    }
};

另请注意,复制/移动构造函数的默认实现不会意外地用于意图 - 因为无法创建抽象基类的实例。因此,您将始终复制派生类,如果复制是合法的,他们应该定义。

但是,对于某些类来说完全复制是没有意义的,在这种情况下,禁止在基类中复制/分配可能是明智的。

Tl;博士:它依赖,但最有可能你最好将它们作为默认值。


0
投票

通常,如果任何大3特殊函数都没有 - [普通/默认]定义,则应定义另外2个特殊函数。如果2个特殊移动函数没有 - [普通默认]定义,那么你需要处理所有5.如果接口有一个nop定义的dtor,你不需要麻烦定义其余的 - 除非其他原因。即使是非平凡的定义也不能使其重新定义其他功能;只有当涉及某种资源管理(例如内存,文件,io,同步......)时,才需要定义大3(5)。

© www.soinside.com 2019 - 2024. All rights reserved.