gpt4 book ai didi

c++ - 为什么在这个程序中没有调用析构函数?

转载 作者:行者123 更新时间:2023-11-28 00:32:50 24 4
gpt4 key购买 nike

<分区>

我做了一个实例化类的函数,并做了一些操作。之后函数返回,这意味着作用域结束了。一旦范围结束,它应该调用类的析构函数。但是,我没有看到析构函数被调用。其背后的原因是什么?我在这里遗漏了什么吗?

/***

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();
}

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com