简体   繁体   English

我们应该为中间继承的类提供默认实现吗

[英]should we provide default implementation for the middle inherited classes

class A
{
public:
    A(…) {…}
    virtual ~A() {…}
    
private:
    // may contains data
};

class B : public A
{
public:
    B(…) {…}
private:
    // contains no data
};

class C : public B
{
public:
    C(…) {…}
    ~C() {…}
private:
    // may contains data
};

As you can see, class A is the base class so we have to provide a implemented virtual destructor.如您所见, class A是基类,因此我们必须提供一个已实现的虚拟析构函数。

Question> Do we have to provide a default destructor for a concrete class B ?问题> 我们是否必须为具体的class B提供默认析构函数? class B itself doesn't require to implement a customized destructor to release any allocated resource. class B本身不需要实现自定义析构函数来释放任何分配的资源。

Thank you谢谢

No. Once one base destructor is virtual, all derived destructors are automatically virtual.不是。一旦一个基础析构函数是虚拟的,所有派生的析构函数都自动是虚拟的。 If the default implementation suffices, you do not need to provide a user-defined destructor.如果默认实现就足够了,则不需要提供用户定义的析构函数。

(In fact, if the default implementation suffices even in the base, you should declare and define it as virtual ~A() = default; , rather than with an empty body.) (事实上​​,如果默认实现即使在基础中也足够,你应该将它声明和定义为virtual ~A() = default; ,而不是一个空的主体。)

No, you don't have to that in case you don't have resources to release.不,如果您没有要发布的资源,则不必这样做。

The same is applied to any other methods including virtual and pure virtual这同样适用于任何其他方法,包括virtualpure virtual

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在默认构造函数中可见的继承类的 c++ 计数器的实现 - implementation of a c++ counter for inherited classes that is visible in default constructor 如果我们使用参数构造函数,我们是否需要在c ++中提供默认构造函数? - Should we need to provide a default constructor in c++ if we use a parameter constructor? 课程默认是最终的吗? - Should classes be final by default? 我们应该提供一个没有抛出说明符的析构函数吗? - Should we provide a destructor with no-throw specifier? C ++抽象类应该为(虚拟)析构函数提供实现吗? - C++ Should an abstract class provide implementation for a (virtual) destructor? 为模板类中的纯虚函数提供默认实现 - Provide default implementation for pure virtual function in a template class 如何为可覆盖的方法提供默认实现? - How can I provide a default implementation for an overridable method? 如果我们将C前缀用于类,是否也应将其用于struct? - If we use the C prefix for classes, should we use it for struct also? 您应该为复制构造函数还是移动构造函数提供默认参数? - Should you provide the copy constructor or the move constructor with a default argument? 包装API时应避免在实现类中进行转换 - Should I avoid upcasting in implementation classes when wrapping an API
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM