C++ 为什么在此程序中不调用析构函数?

C++ 为什么在此程序中不调用析构函数?,c++,C++,我制作了一个函数来实例化这个类,并执行一些操作。函数返回后,这意味着作用域结束。一旦作用域结束,它应该调用类的析构函数。但是,我看不到调用析构函数。背后的原因是什么?我是不是遗漏了什么 /*** The static member in the class is just a declaration, whose namespace scope is limited to the class. One need to define it in the corresponding source

我制作了一个函数来实例化这个类,并执行一些操作。函数返回后,这意味着作用域结束。一旦作用域结束,它应该调用类的析构函数。但是,我看不到调用析构函数。背后的原因是什么?我是不是遗漏了什么

/***

The static member in the class is just a declaration, whose namespace scope is limited to the class.
One need to define it in the corresponding source of the class header. This way, the static member is
allocated a space on the Data segment. In case the static member is not defined, then the linker will
throw an error.

**/

class category_to_subcategory
{
     /** static members have just declaration not definition **/
     static QMultiMap<QString,QString>m_cat_to_subcat;

 public:
     /** The const can give a definition to it within the class, thus no need to define it **/
     /** C++ allows only "integer const static" types to be defined within a class */
     /** both are same const static or static const **/
     const static  int categ_const = 10;
     static void insert_subcat();
     /** template **/
     QList<QString> get_subcat(QString &cat);
     QList<QString> get_subcat(const QString &cat);
     ~category_to_subcategory();
};

/** When you want to map a string to string, you can use QMultimap **/
/** It is quite easy to get the values using QMultimap **/
/** definition of m_cat_to_subcat, the namescope is bind to the class name - category_to_subcategory  **/
QMultiMap<QString,QString> category_to_subcategory::m_cat_to_subcat;

 void category_to_subcategory::insert_subcat()
 {
     qDebug()<<__PRETTY_FUNCTION__;
     m_cat_to_subcat.clear();
     m_cat_to_subcat.insert("OS", "Linux");
     m_cat_to_subcat.insert("OS", "Windows");
     m_cat_to_subcat.insert("OS", "MAC");
     m_cat_to_subcat.insert("SHELL", "BASH");
     m_cat_to_subcat.insert("SHELL", "KSH");
     m_cat_to_subcat.insert("SHELL", "CSH");
     m_cat_to_subcat.insert("MOUSE", "WIRED");
     m_cat_to_subcat.insert("MOUSE", "WIRELESS");

 }

 QList<QString> category_to_subcategory::get_subcat(QString &cat)
 {
     qDebug()<<__PRETTY_FUNCTION__;
     if(category_to_subcategory::m_cat_to_subcat.empty()) {
          category_to_subcategory::insert_subcat();
      }
      QList<QString> subcat_list = m_cat_to_subcat.values(cat);
      return subcat_list;
 }

 QList<QString> category_to_subcategory::get_subcat(const QString &cat)
 {
     qDebug()<<__PRETTY_FUNCTION__;
     if(category_to_subcategory::m_cat_to_subcat.empty()) {
          category_to_subcategory::insert_subcat();
     }
     QList<QString> subcat_list = m_cat_to_subcat.values(cat);
     return subcat_list;
 }

category_to_subcategory::~category_to_subcategory()
{
   qDebug()<<__PRETTY_FUNCTION__;
   delete this;
}

void function_to_QMAP()
{
    qDebug()<<__PRETTY_FUNCTION__;
    category_to_subcategory *cat_to_subcat_Instance = new category_to_subcategory;
    QString cat = "OS";
    qDebug()<<cat_to_subcat_Instance->get_subcat(cat);
    cat = "SHELL";
    qDebug()<<cat_to_subcat_Instance->get_subcat(cat);
    cat = "MOUSE";
    qDebug()<<cat_to_subcat_Instance->get_subcat(cat);
    /** Passing just the string will throw an error **/
    //qDebug()<<cat_to_subcat_Instance->get_subcat("MOUSE");
    /** no matching function for call to 'category_to_subcategory::get_subcat(const char[6]); */

    qDebug()<<"The const category is"<<cat_to_subcat_Instance->get_subcat("OS");
    qDebug()<<"The static const integer defined in class value is "<<category_to_subcategory::categ_const;
}

void function_op_finish()
{
    qDebug()<<__PRETTY_FUNCTION__;
}

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    function_to_QMAP();
    function_op_finish();
    return a.exec();
}
/***
类中的静态成员只是一个声明,其命名空间范围仅限于类。
需要在类头的相应源中定义它。这样,静态成员是
在数据段上分配了一个空间。如果未定义静态成员,则链接器将
抛出一个错误。
**/
类别类别到子类别
{
/**静态成员只有声明而没有定义**/
静态QMultiMapm_cat_to_subcat;
公众:
/**常量可以在类中给它定义,因此不需要定义它**/
/**C++允许只在类**中定义“整数const static”类型。
/**两者都是相同的常量static或static const**/
常数静态整数类别常数=10;
静态空白插入_subcat();
/**模板**/
QList get_subcat(QString和cat);
QList get_subcat(常量QString和cat);
~category_to_subcategory();
};
/**要将字符串映射到字符串时,可以使用QMultimap**/
/**使用QMultimap很容易获得值**/
/**m_cat_to_subcat的定义,名称范围绑定到类名-category_to_subcategory**/
QMultiMap category_to_subcategory::m_cat_to_subcategory;
作废类别到子类别::插入子类别()
{

qDebug(),因为您没有
删除
cat\u to\u subcat\u实例。这是内存泄漏:对象已创建,但从未销毁

在您的特定代码片段中,我认为不需要动态地将
cat\u分配给\u subcat\u实例

void function_to_QMAP()
{
    qDebug()<<__PRETTY_FUNCTION__;
    category_to_subcategory cat_to_subcat_Instance;
    QString cat = "OS";
    qDebug()<<cat_to_subcat_Instance.get_subcat(cat);
    cat = "SHELL";
    qDebug()<<cat_to_subcat_Instance.get_subcat(cat);
    cat = "MOUSE";
    qDebug()<<cat_to_subcat_Instance.get_subcat(cat);
    /** Passing just the string will throw an error **/
    //qDebug()<<cat_to_subcat_Instance.get_subcat("MOUSE");
    /** no matching function for call to 'category_to_subcategory::get_subcat(const char[6]); */

    qDebug()<<"The const category is"<<cat_to_subcat_Instance.get_subcat("OS");

    qDebug()<<"The static const integer defined in class value is "<<category_to_subcategory::categ_const;


}
void函数到QMAP()
{

qDebug(),因为您没有
删除
cat\u to\u subcat\u实例。这是内存泄漏:对象已创建,但从未销毁

在您的特定代码片段中,我认为不需要动态地将
cat\u分配给\u subcat\u实例

void function_to_QMAP()
{
    qDebug()<<__PRETTY_FUNCTION__;
    category_to_subcategory cat_to_subcat_Instance;
    QString cat = "OS";
    qDebug()<<cat_to_subcat_Instance.get_subcat(cat);
    cat = "SHELL";
    qDebug()<<cat_to_subcat_Instance.get_subcat(cat);
    cat = "MOUSE";
    qDebug()<<cat_to_subcat_Instance.get_subcat(cat);
    /** Passing just the string will throw an error **/
    //qDebug()<<cat_to_subcat_Instance.get_subcat("MOUSE");
    /** no matching function for call to 'category_to_subcategory::get_subcat(const char[6]); */

    qDebug()<<"The const category is"<<cat_to_subcat_Instance.get_subcat("OS");

    qDebug()<<"The static const integer defined in class value is "<<category_to_subcategory::categ_const;


}
void函数到QMAP()
{

qDebug(),因为您没有
删除
cat\u to\u subcat\u实例。这是内存泄漏:对象已创建,但从未销毁

在您的特定代码片段中,我认为不需要动态地将
cat\u分配给\u subcat\u实例

void function_to_QMAP()
{
    qDebug()<<__PRETTY_FUNCTION__;
    category_to_subcategory cat_to_subcat_Instance;
    QString cat = "OS";
    qDebug()<<cat_to_subcat_Instance.get_subcat(cat);
    cat = "SHELL";
    qDebug()<<cat_to_subcat_Instance.get_subcat(cat);
    cat = "MOUSE";
    qDebug()<<cat_to_subcat_Instance.get_subcat(cat);
    /** Passing just the string will throw an error **/
    //qDebug()<<cat_to_subcat_Instance.get_subcat("MOUSE");
    /** no matching function for call to 'category_to_subcategory::get_subcat(const char[6]); */

    qDebug()<<"The const category is"<<cat_to_subcat_Instance.get_subcat("OS");

    qDebug()<<"The static const integer defined in class value is "<<category_to_subcategory::categ_const;


}
void函数到QMAP()
{

qDebug(),因为您没有
删除
cat\u to\u subcat\u实例。这是内存泄漏:对象已创建,但从未销毁

在您的特定代码片段中,我认为不需要动态地将
cat\u分配给\u subcat\u实例

void function_to_QMAP()
{
    qDebug()<<__PRETTY_FUNCTION__;
    category_to_subcategory cat_to_subcat_Instance;
    QString cat = "OS";
    qDebug()<<cat_to_subcat_Instance.get_subcat(cat);
    cat = "SHELL";
    qDebug()<<cat_to_subcat_Instance.get_subcat(cat);
    cat = "MOUSE";
    qDebug()<<cat_to_subcat_Instance.get_subcat(cat);
    /** Passing just the string will throw an error **/
    //qDebug()<<cat_to_subcat_Instance.get_subcat("MOUSE");
    /** no matching function for call to 'category_to_subcategory::get_subcat(const char[6]); */

    qDebug()<<"The const category is"<<cat_to_subcat_Instance.get_subcat("OS");

    qDebug()<<"The static const integer defined in class value is "<<category_to_subcategory::categ_const;


}
void函数到QMAP()
{

似乎你混淆了C++中基于栈的两种内存分配(在栈中分配了局部变量,当函数退出时自动销毁)和堆(用“新”分配,程序员负责确保某点删除)/<

这里有一个不同之处,欢迎大家在C++中手动管理内存的乐趣和悲伤(这可能是语言中错误和问题的最大来源)注意,链接是专门针对C而不是C++的,但是在两个概念中都是相同的。

< P>看来,你混淆了C++中基于栈的两种内存分配(在堆栈上分配局部变量,当函数退出时自动销毁)和堆为基础。(使用“new”分配,程序员负责确保在某个时候调用“delete”)


这里有一个不同之处,欢迎大家在C++中手动管理内存的乐趣和悲伤(这可能是语言中错误和问题的最大来源)注意,链接是专门针对C而不是C++的,但是在两个概念中都是相同的。

< P>看来,你混淆了C++中基于栈的两种内存分配(在堆栈上分配局部变量,当函数退出时自动销毁)和堆为基础。(使用“new”分配,程序员负责确保在某个时候调用“delete”)


这里有一个不同之处,欢迎大家在C++中手动管理内存的乐趣和悲伤(这可能是语言中错误和问题的最大来源)注意,链接是专门针对C而不是C++的,但是在两个概念中都是相同的。

< P>看来,你混淆了C++中基于栈的两种内存分配(在堆栈上分配局部变量,当函数退出时自动销毁)和堆为基础。(使用“new”分配,程序员负责确保在某个时候调用“delete”)


这里有一个不同之处,欢迎大家在C++中手动管理内存的乐趣和悲伤(这可能是语言中错误和问题的最大来源)注意,链接是专门针对C而不是C++,但是在两个概念中都是相同的。

< P>,使用操作符<代码>新< /COD>
category_to_subcategory *cat_to_subcat_Instance = new category_to_subcategory;
要删除此对象,需要显式调用运算符
delete

还要考虑到类category_to_subcategory的析构函数无效

category_to_subcategory::~category_to_subcategory()
{
   qDebug()<<__PRETTY_FUNCTION__;
   delete this;
}
category_to_subcategory::~category_to_subcategory()
{

qDebug()您使用操作符
new
将category\u类型的对象分配给堆中的\u子类别

category_to_subcategory *cat_to_subcat_Instance = new category_to_subcategory;
要删除此对象,需要显式调用运算符
delete

还要考虑到析构函数