deleting object with template for int and object
Posted
by
Yokhen
on Stack Overflow
See other posts from Stack Overflow
or by Yokhen
Published on 2012-10-24T22:45:48Z
Indexed on
2012/10/24
23:00 UTC
Read the original article
Hit count: 242
Alright so Say I have a class with all its definition, 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
};
And then I have in the main method:
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;
}
My question is: What could I do to have both delete statements in the main method work just fine?
P.S.: Although I have not actually compiled/tested this specific code, I have reviewed it extensively (as well as indented. You're welcome.), so if you find a mistake, please be nice. Thank you.
© Stack Overflow or respective owner