Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


Don't forget to read Tuesday's email newsletter for your chance to win a free copy of Construct 2!


What methods should my destructor contain?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
6 replies to this topic

#1 stitchs   Crossbones+   -  Reputation: 1307

Like
0Likes
Like

Posted 02 September 2012 - 05:58 AM

Hello,

This is just a quick question but it has been bugging me a while. I have a GameEngine class (probably should be renamed to something less Grande). I have it set so that, before it does all the standard stuff like deleting pointers, destroying bitmaps, it saves the current game. Snippet below:
[source lang="cpp"]GameEngine::~GameEngine(void){ _SaveData("GameData.txt", a_PlayerOne->f_GetScore(), a_PlayerTwo->f_GetScore()); // For every new/BITMAP*, there must be a matching delete/destroy. delete a_Grid; delete a_PlayerOne; delete a_PlayerTwo; destroy_bitmap(a_PlayerO); destroy_bitmap(a_PlayerX); // TODO: (Shape that appears underneath cursor /*if(shapeCursor != NULL){ destroy_bitmap(shapeCursor); }*/ destroy_bitmap(temp); // TODO: destroy_bitmap(a_Buffer);}[/source]
Is this the correct thing I should be doing, or should I make an explicit call to it in the code, before the destructor is run? Are there any dangers of leaving it until the class destructs?

Regards,

Stitchs.

Sponsor:

#2 Álvaro   Crossbones+   -  Reputation: 13659

Like
4Likes
Like

Posted 02 September 2012 - 06:45 AM

My main problem with that code is that another programmer reading the code that uses this class will not expect that the destructor will save the game to a file, because it's unusual for a destructor to do such things.

Another problem is the hard-coded filename. If you wanted to make it more flexible so you could save the game somewhere else, you would need to pass the filename as an argument, but the destructor doesn't take arguments. This is another indication that saving should be done in a method, not the destructor.

#3 SiCrane   Moderators   -  Reputation: 9628

Like
4Likes
Like

Posted 02 September 2012 - 07:05 AM

Because of stack unwind semantics, C++ destructors should not contain any function calls that can fail when passed valid arguments. Freeing resources is fine. Any function that needs to allocate resources in order to function, like acquire a file handle, is not.

On another note, identifiers beginning with an underscore followed by a capital letter are reserved for the implementation. Using such identifiers in your own code can cause problems on different platforms.

#4 stitchs   Crossbones+   -  Reputation: 1307

Like
0Likes
Like

Posted 02 September 2012 - 01:15 PM

Thanks for the swift responses. I had a feeling that how I had done it was incorrect, I just wanted to double check and couldn't word my question correctly in a net search.

I'll move into implementing some sort of close-down function that is called independently of the destructor.

Regards,

Stitchs

#5 thedevsykes   Members   -  Reputation: 336

Like
2Likes
Like

Posted 02 September 2012 - 04:02 PM

What you did wasn't incorrect, its just bad practice effectively. As its already been pointed out, if I was to use your class, I would not expect when it went out of scope/or I deleted it, that it would save a game file. My port of call is to create classes as if they are intended to be used by someone else, unless of course, those classes will never be used by someone else. If that makes any lick of sense .... hmm.

Edited by theark, 02 September 2012 - 05:23 PM.

'Knowledge isn't key, but understanding...'

My qualifcations are not here to showcase, but for those I answer and ask, to get a better idea on my knowledge.

BCS Level 2 Certificate for IT Users (ECDL Part 2)
OCR Level 2 National Award in Business
Level 2 First Diploma in Media
Level 3 Diploma in Games Design and Development Extended
BSc Hons in Computer Games Programming (Current - 1st Year)

#6 Servant of the Lord   Crossbones+   -  Reputation: 20348

Like
1Likes
Like

Posted 02 September 2012 - 04:21 PM

As its already been pointed out, if I was to use your class, I would not expect when it went out of scope/or I deleted it, that it would save a game file.

(Corrected, to avoid potential confusion from the missing word)
It's perfectly fine to abbreviate my username to 'Servant' rather than copy+pasting it all the time.
All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.
Of Stranger Flames - [indie turn-based rpg set in a para-historical French colony] | Indie RPG development journal

[Fly with me on Twitter] [Google+] [My broken website]

[Need web hosting? I personally like A Small Orange]


#7 thedevsykes   Members   -  Reputation: 336

Like
0Likes
Like

Posted 02 September 2012 - 05:22 PM


As its already been pointed out, if I was to use your class, I would not expect when it went out of scope/or I deleted it, that it would save a game file.

(Corrected, to avoid potential confusion from the missing word)


Thanks, must have missed that when reading it back. :)
'Knowledge isn't key, but understanding...'

My qualifcations are not here to showcase, but for those I answer and ask, to get a better idea on my knowledge.

BCS Level 2 Certificate for IT Users (ECDL Part 2)
OCR Level 2 National Award in Business
Level 2 First Diploma in Media
Level 3 Diploma in Games Design and Development Extended
BSc Hons in Computer Games Programming (Current - 1st Year)




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS