有一个创建的类抛出一个new
关键字和一个指向它的指针,然后从该类调用一个方法来销毁它自己的类,我是否必须从指针中释放内存?像这样的东西:
。H
#include <list>
#include <iostream>
class Test
{
public:
Test();
~Test();
void destroy();
bool operator==(Test& right){return id == right.id;};
private:
static int count;
std::list<Test*>::size_type id;
static std::list<Test*> instances;
};
.cpp
#include "Test.h"
int Test::count = 0;
std::list<Test*> Test::instances;
Test::Test()
{
instances.push_back(this);
id = count++;
}
Test::~Test()
{
}
void Test::destroy()
{
for(auto t : instances){
if(*t == *this)
{
delete t;
break;
}
}
}
资源
Test* n = new Test();
n->destroy();
所以我的问题是 n 指向的内存被释放了吗?还是我应该打电话delete n