This topic is now archived and is closed to further replies.



Recommended Posts

Hi I have some trouble with "delete"... if you have a class like this: class B { private: int *index; public: B(){}; ~B(){delete[] index;} void func(){index = new index[5];} }; And a little program doing this: void main() { B *b; b = new B[1]; b[0].func(); delete[] b; } This would work fine... but if you took away the call to "func" it would crash when the destructor is called because no memory has been allocated for index. My question is how should I write the destructor so that it only deletes index if you have allocated memmory for index.

Share this post

Link to post
Share on other sites