Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

AppGameKit Classic Chat / Do I have to manually delete object even after destructor? [TIER 2 - C++]

Author
Message
Daqs
9
Years of Service
User Offline
Joined: 23rd May 2015
Location:
Posted: 5th Jun 2015 09:42 Edited at: 5th Jun 2015 09:45
This question is related to Tier 2 - C++

I created a separate class for Text so that I don't have to call agk functions when creating Text. Here's the simple class:

// Text.h

Now my question is when I'm calling this class in any other class, say MainMenu, do I have to delete the button in the class MainMenu that I'm creating using this class or will the destructor of Text will automatically get called and delete the button.

// MainMenu.cpp


Peronally, I think deleting the button in MainMenu should be unnecessary. Please let me know if you think I'm wrong. Thanks!
Lucas Tiridath
AGK Developer
16
Years of Service
User Offline
Joined: 28th Sep 2008
Location: Kings Langley, UK
Posted: 6th Jun 2015 01:03 Edited at: 6th Jun 2015 01:04
Hi Daqs. As you say in your question, you don't need to call agk :: DeleteText(1); after your text object is destroyed, because your text object does the exact same thing in its destructor. The problem you have in this code is that the Text destructor on mainText is never called, because you never delete it.

Instead, in your destructor for MainMenu, I would expect to see something like this.


Having said that, I'm a little confused by what you're trying to achieve here. Anyhow I hope that helps.

Daqs
9
Years of Service
User Offline
Joined: 23rd May 2015
Location:
Posted: 8th Jun 2015 10:47
Lucas,

Thanks for your reply. What you said made complete sense. What if I'm using smart pointer (std::unique_ptr to be specific). Then how does it work? because in that case I won't be calling the delete to the pointer mainText being created in MainMenu().

Would then the destructor get called automatically?

Login to post a reply

Server time is: 2024-11-16 18:15:39
Your offset time is: 2024-11-16 18:15:39