Archived

This topic is now archived and is closed to further replies.

Constructors and Destrctors?

Recommended Posts

What exactly is a Constructor/Destructor, ive looked at them in tutorrials and i still dont understand what they do? Homepage Luck is a Horse to ride like any other...Luckily im not a gambler, I dont know how to ride. For any total noob out there Here is a like to my tutorials, thry them out

Share on other sites
alnite    3437
Constructor is a special function called when an object is instantiated.
Destructor is a special function called when an object is uninstantiated.

Current project: A puzzle game.
% completed: ~0%
Status: Active.

Share on other sites
micepick    122
A constructor is called (automatically) whenever a new object is created. A destructor is called when an object is destroyed (i.e. goes out of scope, or is deleted).

Share on other sites
Johnny_Null    122
For example, you could use a constructor to set member values when you create an instance of a class. A destructor could be used to free dynamically allocated data when an instance of your class/struct/whatever goes out of scope.

[edited by - Johnny_Null on April 28, 2003 7:20:12 PM]

Share on other sites
cleves    148
You must not forget to put a Destructor or you will have a serious memory leaks

Share on other sites
Guest Anonymous Poster
Sorry if Im wrong, but I believe you have to explain for him what an object is as well

Share on other sites
idko    122
Say we have this class:

class poo
{
int iStinklevel;
int iSize;
};

A class definition only defines the structure of the object, but it does not create an actual object. When your program declares an object of type poo, the program reserves 8 bytes of space for your object(since windows uses 4 byte integers).

void main(void)
{
poo myPoo;
}

So now you have an object of the poo type and you can reference the iStinklevel and the iSize using myPoo.iStinklevel and myPoo.iSize respectively. iStinklevel and iSize at this point hold whatever random value was already in its memory spot. Because it is not allowed to initialize the variables in the class definition itself, you must use a Constructor. A constructor is just a function that is called every time an object is created. It has the same name as your class.

Say for example that you wanted iStinklevel and iSize to be zero by default. You would initialize it in the constructor like this:

class poo
{
int iStinklevel;
int iSize;
public:
poo()
{
iStinklevel = 0;
iSize = 0;
}
};

or

class poo
{
int iStinklevel;
int iSize;
public:
poo():iStinlevel(0),iSize(0){}
};

The destructor is almost the same thing. It is called when the object''s life is over. When you explicitly delete an object or when its scope is over. It has the same name as the class and a tilde ~ in front of it.

~poo()
{
// and you do some stuff here like free memory and such.
}

So, I hope that helped you a bit. Just remember that constructors and destructors are useful for initializing stuff and deleting stuff. Of course, you can do whatever else you want with them. There''s really no limit. One common use of them is allocating memory and releasing memory. That''s useful also because objects can take parameters.

ex:

class poo
{
char *szPooName;
int iStinklevel;
int iSize;
public:
poo(char *name)
{
int length = strlen(name); // figure out how long name is
szPooName = (char *)malloc(length); // allocate memory for szPooName

// szPooName = name; won''t work because they are just pointers
strcpy(szPooName, name); // this will copy what''s in name into szPooName
}

~poo()
{
free(szPooName); // free the memory used by szPooName
}
};

and there you have it.