我在使用带有 std::map 的自定义类时遇到问题。该类为成员动态分配内存,我不想在映射中使用指针,因为我想确保该类负责删除所有分配的内存。但是我遇到的问题是在我将项目添加到地图之后,当该代码块超出范围时,即使它仍在地图上,也会调用对象析构函数。我在下面做了一段假代码来说明我的意思。输出是:那么问题是为什么要调用最终的析构函数?在此先感谢,很抱歉这个问题很长。
Constructor Called Num:0034B7E8
Default Constructor Called Num:00000000
Copy Constructor Called Num:CCCCCCCC
Copy Constructor Called Num:CDCDCDCD
destructor called Num:CCCCCCCC
destructor called Num:00000000
destructor called Num:0034B7E8
Inserted Num:0034B7E8
class myClass
{
public:
myClass(int num)
{
mnNum = new int();
cout << "Constructor Called Num:" << mnNum << endl;
}
myClass() : mnNum(NULL)
{
cout << "Default Constructor Called Num:" << mnNum << endl;
}
myClass(const myClass ©)
{
mnNum = new int(copy.mnNum);
cout << "Copy Constructor Called Num:" << mnNum << endl;
}
~myClass()
{
delete mnNum;
mnNum = NULL;
}
int* mnNum;
};
map<string,myClass> mvMyMap;
void testFunction()
{
myClass lcObj(1);
mvMyMap["Test"] = lcObj;
}
int _tmain(int argc, _TCHAR* argv[])
{
testFunction();
cout << "Inserted Num:" << mvMyMap["Test"].mnNum << endl;
return 0;
}