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.

Dark GDK / Constructors / Destructors

Author
Message
wickedly kick it
18
Years of Service
User Offline
Joined: 13th Jul 2006
Location: Fort-worth Texas
Posted: 1st Aug 2009 21:00
What exactly do they do, i just heard that they allow you to create and destroy objects to save memory. Should i use them?

Cole Xemi
16
Years of Service
User Offline
Joined: 20th Aug 2008
Location:
Posted: 1st Aug 2009 23:48 Edited at: 1st Aug 2009 23:53
The Constructor is called when you create an object of a class.
The Destructor is called when you delete the object.

The Constructor is useful for setting up a class.
The Destructor should destroy anything that has been allocated to avoid dangling pointers and memory leaks. Lets say you need to call a function in your game when a creature dies(gets deleted), a good place to put the function call is in the creatures destructor.

http://www.cplusplus.com/doc/tutorial/classes/
wickedly kick it
18
Years of Service
User Offline
Joined: 13th Jul 2006
Location: Fort-worth Texas
Posted: 2nd Aug 2009 01:01
Thanks, for something like a player object, would i need them? becaus the player is always going to be set up.

Cole Xemi
16
Years of Service
User Offline
Joined: 20th Aug 2008
Location:
Posted: 2nd Aug 2009 01:30
I think it would be a good idea to implement a constructor/destructor for the player even though the player my not die(get deleted) in game. The Player will be destroyed when the game is exited.

Lets say you can play as multiple characters on different levels, when switching between characters you would first need to clean up what you created.

Always delete what you create, even DarkGDK objects!
Bubzy
15
Years of Service
User Offline
Joined: 29th Jun 2009
Location: somewhere
Posted: 2nd Aug 2009 01:35 Edited at: 2nd Aug 2009 01:37
i would use a constructor because its an easy way to set all of your player parameters on one line

so instead of something like

void makeplayer()
{
player.id = 1;
player.Xpos = 10.0f;
player.Ypos = 10.0f;
player.Zpos = 10.0f;
player.speed = 2.4f;
}

in your main code, you could write this in your class :

player::player(int Id,float X, float Y, float Z,float Speed)
{
Xpos = X;
Ypos = Y;
id = Id;
speed = Speed;
dbMakeObjectCube(3,5);
dbPositionObject(Id,X,Y,Z);
}


using the constructor you can now create your player like this in your main code

player Player1(1,10.0f,10.0f,10.0f,2.4f);
player Player2(2,10.0f,10.0f,20.0f,2.4f);
etc....

destructors are useful and you can use that to clean up anything that is still in memory

player::~player()
{
dbDeleteObject(id);
}

its best to read up on a good tutorial about constructor/destructor uses. they are handy though. hope this helped a little bit (im not the best example giver or coder)

void Void(void){(for i = 0; i < i+1; i++){cout<<"bubzy rules "};}
wickedly kick it
18
Years of Service
User Offline
Joined: 13th Jul 2006
Location: Fort-worth Texas
Posted: 2nd Aug 2009 03:15
Thanks
Im curious... with classes you can access variables like that?
(player.xpos=blah) Didnt know that should read the tutorials better xD

Bubzy
15
Years of Service
User Offline
Joined: 29th Jun 2009
Location: somewhere
Posted: 2nd Aug 2009 10:44
yeah you can access the variables like that.
you can make the variables private so that they can only be accessed by the class. or public so you can call them from anywhere.

void Void(void){(for i = 0; i < i+1; i++){cout<<"bubzy rules "};}

Login to post a reply

Server time is: 2024-10-01 08:38:42
Your offset time is: 2024-10-01 08:38:42