0
嗯,我正在学习的模板和,我有下一个代码问题:删除[]析构函数
#include <iostream>
using namespace std;
template<class T, int n>
class Table
{
public:
Table();
//~Table();
int& operator[](int i);
bool Resize(int n);
int Count();
void Free();
private:
T* inst;
int count;
};
template<class T, int n>
Table<T, n>::Table()
{
inst = new T[n];
count = n;
}
template<class T, int n>
void Table<T, n>::Free()
{
delete[] this->inst;
}
template<class T, int n>
int& Table<T, n>::operator[](int i)
{
return inst[i];
}
template<class T, int n>
bool Table<T, n>::Resize(int n)
{
this->inst = (T*)realloc(this->inst, sizeof(T)*count + sizeof(T)*n);
if(!inst)
return false;
return true;
}
template<class T, int n>
int Table<T, n>::Count()
{
return this->count;
}
template<typename T, int n> void ShowTable(Table<T, n> t)
{
for(int i=0; i<t.Count(); i++)
cout<<t[i]<<endl;
}
int main()
{
Table<int, 2> table;
table[0] = 23;
table[1] = 150;
ShowTable(table);
system("pause");
table.Free();
return 0;
}
它的工作原理,但是,当我把delete[] this->inst;
在析构函数,它抛出我一个声明失败,我不知道为什么......我的意思是,删除析构函数中的资源是不好的?
谢谢,但我改变了该参数的名称,我仍然得到断言失败时,我写:delete [] this-> inst;在析构函数的定义 – German 2012-08-15 01:48:37
您的代码在更改参数的名称后为我工作。你得到的实际信息是什么? (断言失败的消息通常附带* some * detail) – 2012-08-15 01:50:03
它引发了我“Expression _BLOCK_TYPE_IS_VALID(pHead-> nBlockUse)”dbgdel.cpp line:52 – German 2012-08-15 01:51:39