简体   繁体   English

为什么要使用虚拟析构函数?

[英]Why virtual destructor?

I am going through some code,plan to adapt it for my research.So header file looks like this 我正在检查一些代码,计划对其进行调整以适合我的研究。因此头文件如下所示

#ifndef SPECTRALCLUSTERING_H_
#define SPECTRALCLUSTERING_H_

#include <vector>
#include <eigen3/Eigen/Core>

class SpectralClustering {
public:
    SpectralClustering(Eigen::MatrixXd& data, int numDims);
    virtual ~SpectralClustering();

    std::vector<std::vector<int> > clusterRotate();
    std::vector<std::vector<int> > clusterKmeans(int numClusters);
    int getNumClusters();

protected:
    int mNumDims;
    Eigen::MatrixXd mEigenVectors;
    int mNumClusters;
};

#endif /* SPECTRALCLUSTERING_H_ */

Latter in the main code 主要代码中的后者

#include "SpectralClustering.h"
#include <eigen3/Eigen/QR>

SpectralClustering::SpectralClustering(Eigen::MatrixXd& data, int numDims):
    mNumDims(numDims),
    mNumClusters(0)

So I do not understand why virtual destructor was used in the .h file. 所以我不明白为什么在.h文件中使用虚拟析构函数。 From this we can learn that virtual destructors are useful when you can delete an instance of a derived class through a pointer to base class.But I think this is not case with this code.Can someone explain all this? 这个我们可以得知,虚析构函数是有用时,你可以通过指针删除派生类的实例,立足class.But我想,这是不是跟这个code.Can有人区分解释这一切?

The reason you would make a destructor virtual is that you plan for that class to be inherited and used polymorphicly. 将析构函数虚拟化的原因是,您计划该类被多态继承和使用。 If we had 如果我们有

class Foo {};
class Bar : public Foo {};

Foo * f = new Bar();
delete f; // f's destructor is called here

The destructor for Foo would be called and no members of the Bar part of the object would be destroyed. Foo的析构函数将被调用,并且对象的Bar部分的任何成员都不会被销毁。 If Foo had a virtual destructor then a vtable lookup would happen the the Bar destructor would be called instead correctly destroying the object. 如果Foo具有虚拟析构函数,则将执行vtable查找,而将调用Bar析构函数来正确销毁对象。

It is supposed that the class can be inherited. 假定可以继承该类。 Otherwise it should be declared with specifier final . 否则,应使用说明符final声明。

Take into account that data members of the class have access control specifier protected . 考虑到该类的数据成员已protected访问控制说明符的protected It means that the author of the class does not exclude that the class can be inherited. 这意味着该类的作者并不排除该类可以被继承。

The code was probably written in such a way, that customizing the class implementations by deriving custom classes is supported. 该代码可能以这样的方式编写,即支持通过派生自定义类来自定义类实现。

The user of the framework can customize the framework in that way without the need to change the framework code directly. 框架的用户可以以这种方式自定义框架,而无需直接更改框架代码。 So, probably there is a way to use your derived class in stead of the original SpectralClustering class, or even in stead of any class SpectralClustering derives from (not in this case, as it does not derive from anything). 因此,可能有一种方法可以使用您的派生类代替原始SpectralClustering类,甚至代替SpectralClustering派生的任何类(在这种情况下,因为它不是从任何东西派生的)。 The framework may very well be deleting instances using base class references, while the actual implementation is derived. 该框架很可能会使用基类引用删除实例,而派生实际的实现。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM