本文介绍了C ++的析构函数类似接口的类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

开始在现有代码库(恐惧和恐惧)上使用PC-Lint。



它抱怨的一件事是:

  class IBatch 
{
public:
virtual void StartBatch()= 0;
virtual int CommitBatch()= 0;
};

当另一个类派生时使用它像接口一样使用它

 基类'IBatch'没有析构函数

所以,问题:当你创建类似上面的接口类,你总是包括一个虚拟析构函数?为什么? (是风格还是编码错误?)



编辑:应该说我不期望或希望IBatch的用户破坏,它们是一个服务的消费者,通过这个接口到一些外部实现类(如果这将有所作为)

解决方案

blockquote>

基类析构函数应该是public和virtual,或受保护和非虚拟。



Starting to use PC-Lint on an existing code base (fear and trepidation).

One thing that it complains about is the following:

 class IBatch
 {
 public:
    virtual void StartBatch() =0;
    virtual int CommitBatch() =0;
 };

Which when another class derives from this to use it like an interface

base class 'IBatch' has no destructor

So, the question: when you create Interface classes like the above, do you always include a virtual destructor? Why? (is it a style or a coding error?)

EDIT: Should have said that I do not expect or want the user of IBatch to destruct, they are a consumer of a service only, through this interface to some external implementing class (if that would make a difference)

解决方案

(Herb Sutter, Guru of the Week #18: "Virtuality")

这篇关于C ++的析构函数类似接口的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-16 03:48