c++ - Why virtual destructor? -


i going through code,plan adapt research.so header file looks 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 main code

#include "spectralclustering.h" #include <eigen3/eigen/qr>  spectralclustering::spectralclustering(eigen::matrixxd& data, int numdims):     mnumdims(numdims),     mnumclusters(0) 

so not understand why virtual destructor used in .h file. this can learn virtual destructors useful when can delete instance of derived class through pointer base class.but think not case code.can explain this?

the reason make destructor virtual plan class inherited , used polymorphicly. if had

class foo {}; class bar : public foo {};  foo * f = new bar(); delete f; // f's destructor called here 

the destructor foo called , no members of bar part of object destroyed. if foo had virtual destructor vtable lookup happen the bar destructor called instead correctly destroying object.


Comments

Popular posts from this blog

html - Firefox flex bug applied to buttons? -

html - Missing border-right in select on Firefox -

python - build a suggestions list using fuzzywuzzy -