Sign in to follow this  
Iccarus

Deleting class data (c++)

Recommended Posts

Iccarus    122
If I want to delete all the data stored in the player class how would I do it?
delete player;
doesn't work. Is there anyway to do it without doing it to every single member individually?

Share this post


Link to post
Share on other sites
jyk    2094
You only delete data that you've allocated with new. For a more detailed answer, we'll probably have to see the player class and how it's created.

Share this post


Link to post
Share on other sites
Iccarus    122
Ok.

My save/load function doesn't work properly if you've either loaded already or started a new game. Therefore I want to free all the data in the player class to stop that from happening. Delete obviously isn't what I want. I want to do it all at once without having to individually do it to each member if possible.

Share this post


Link to post
Share on other sites
raz0r    586
Quote:
Original post by Iccarus
I don't have a destructor. I didn't think one was needed when not using win32. This is a commandline text game btw.


A class destructor has nothing to do with Win32...
Still though, why not make a function to clean up all of your data? Something like CPlayer::Reset() if you don't want to use a destructor?

Share this post


Link to post
Share on other sites
Iccarus    122
I don't know anything about destructing or cleaning data. This is my first project. How do I destroy the things (which is my orignal question anyway)

Share this post


Link to post
Share on other sites
raz0r    586

class CDummy
{
public:
CDummy(void) //This is the class constructor
{
//1
};

~CDummy(void) //This is the class destructor
{
//3
};

void Dummy(void)
{
//2
};
};

...

CDummy * pDummy = new CDummy;

pDummy->Dummy();

delete pDummy;










new calls the constructor, delete calls the destructor...
Always delete what you new, otherwise you've got yourself a memory leak...

Keep in mind that the above allocates space on the heap.

Also, you should probably read up on OOP, give this a read.

Share this post


Link to post
Share on other sites
Tesl    211
It depends on how your classes are written, and where the data is being stored. If your allocating data on the stack (ie without calling new to create an object) then it will delete itself without a problem.

If you created the object using new, then you have to call delete on it somewhere else. If you have used new in an objects constructor, it should delete it in the destructor.

If your talking about something else entirely, then we need a bit more information :)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this