好吧,假设我有一门课程,其所有定义,bla bla bla ...
template <class DT>
class Foo{
private:
DT* _data;
//other stuff;
public:
Foo(DT* data){ _data = data; }
virtual ~Foo(){ delete _data; }
//other methods
};
然后我在主要方法中有:
int main(){
int number = 12;
Foo<anyRandomClass>* noPrimitiveDataObject = new Foo<anyRandomClass>(new anyRandomClass());
Foo<int>* intObject = new Foo<int>(number);
delete noPrimitiveDataObject; //Everything goes just fine.
delete intObject; //It messes up here, I think because primitive data types such as int are allocated in a different way.
return 0;
}
我的问题是:我该怎么做才能让 main 方法中的两个 delete 语句都能正常工作?
PS:虽然我没有实际编译/测试过这个特定的代码,但我已经对它进行了广泛的审查(以及缩进),所以如果你发现错误,请多多关照。