我在使用带有std :: map的自定义类时遇到问题。该类为成员动态分配内存,我不想在地图中使用指针,因为我想确保该类负责删除所有已分配的内存。但我遇到的问题是在我将项目添加到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;
}
答案 0 :(得分:10)
myClass
还需要一个自定义赋值运算符。因此,当您进行分配时,您将泄漏左侧的原始值,并最终双重删除右侧的值。
答案 1 :(得分:-1)
您的构造函数会忽略num
参数,并且永远不会从中初始化mnNum
。它应该看起来像:
myClass(int num)
{
mnNum = new int(num);
cout << "Constructor Called Num:" << mnNum << endl;
}
您还需要像这样调整复制构造函数:
myClass(const myClass ©)
{
mnNum = new int(*copy.mnNum);
cout << "Copy Constructor Called Num:" << mnNum << endl;
}
修改的
Derek Ledbetter指出我们也需要一个赋值运算符。我建议将析构函数设为虚拟。