Skip to main content
GameDev.net gamedev.net
🔒 Locked

Deconstructors

Started by MARS_999 Feb 16, 2003 at 10:45 PM 7 replies 300+ views
Original Post
MARS_999
MARS_999
I was wondering is their any harm in calling a deconstructor to destroy an object? Will that destroy an object that isn''t allocated with new? Or will this cause memory leaks? I don''t have dynamic objects setup yet and instead of recoding a bunch of code right now I would like to be able to destroy an object that isn''t dynamicaly allocated. Thanks
LodeRunner
LodeRunner
It won''t deallocate the memory. But if you inherited from any other classes that contain virtual destructors, they won''t be called at all. (As would be expected in normal object destruction.)
Miserable
Miserable
Generally, you should never explicitly call a destructor, but instead rely on the delete and delete[] operators to do it for you. Why would you want to?
MARS_999
MARS_999
quote:
Original post by Miserable
Generally, you should never explicitly call a destructor, but instead rely on the delete and delete[] operators to do it for you. Why would you want to?


I don''t have my objects declared using new to allocate memory.


  
FOO *foo = new FOO;
//not using that way


FOO foo = FOO(stuff);
//using this way



Problem is once an enemy is destroyed how do I delete the object beings I am not using new? This object is global also. Hope that clears up what I am doing. Thanks
Sailorstick
Sailorstick
Then there is no need to delete. Foo will be deleted when the program exits (or any function where Foo is declared exits)


''Your theory of a donut shaped universe is intriguing Homer'' - Stephen Hawking
MARS_999
MARS_999
quote:
Original post by Sailorstick
Then there is no need to delete. Foo will be deleted when the program exits (or any function where Foo is declared exits)


''Your theory of a donut shaped universe is intriguing Homer'' - Stephen Hawking


I know that, I want to delete the object if something happens not when the function or program ends. I have the object as a global so that kind of eliminates when the function exits. Thanks for the help though.
RhoneRanger
RhoneRanger



No there isnt, it will work although this is not the best way to
handle something like this.

Techlead
www.myran.com/enilno
TechleadEnilno, the Ultima 2 projectwww.dr-code.org/enilno
MARS_999
MARS_999
Great, now onto recoding everything. Oh BTW if you would please read my other post on Arrays and new. Thanks
RhoneRanger
RhoneRanger
quote:
Original post by MARS_999
I was wondering is their any harm in calling a deconstructor to destroy an object? Will that destroy an object that isn''t allocated with new? Or will this cause memory leaks?




No there isnt, it will work although this is not the best way to
handle something like this.


Techlead
www.myran.com/enilno
TechleadEnilno, the Ultima 2 projectwww.dr-code.org/enilno

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.