Advertisement

Constructors and Destructors

Started by May 13, 2002 05:45 PM
5 comments, last by Dario 22 years, 9 months ago
Ive read a few books, and source code that explain constructors and destructors in classes, but its still kind of confusing to me, and i aint sure what exactly its used for, so if anyone knows of an easier way to explain them please help!!!!
constructors initialize the class when it is created.
destructors are online usefull if you have allocated memory within the class and it needs to be explicitely deleted.

want to know more? Try to ask more specific questions though ^_^ .sen
"I want to make a simple MMORPG first" - Fenryl
Advertisement
first question is do you understand pointers? if not read up on that.

second question: do you understand what a class is? if not read up on that.

if yes to both questions:

constructor makes an instance in memory of an object/class. you can define one for yourself that accepts different parameters to differently initialize the member variables of that object/class. it returns a pointer to the memory space in which the new object was created. if you don''t define one, C++ will privide an empty constructor for you that allocates enough memory to hold your object but it won''t initialize any of your member variables.

destructor kills all associated memory of an object. if you allocate any special memory inside the class like arrays, other objects, etc, you write the destructor such that all that memory is freed and you don''t get memory leaks.

example class with constructors/destructor:

class Base {protected:    int var1;    int var2;    int * myArray;public:    //basic constructor;    Base() {        var1 = 0;        var2 = 0;        myArray = NULL;    }    //more complex constructor    Base(int _var1, int var2, int arraySize) {        var1 = _var1;        var2 = _var2;        myArray = new int[arraySize];    }    //destructor    ~Base() {        //if myArray is not NULL        if (myArray)            delete[] myArray;    }}; 


usage:

int main(void) {    //get a Base object with everything set to 0 or NULL    Base * foo = new Base();    //get a Base object with variables initialized    Base * poo = new Base(1,2,5);    //at end of fctn delete all the memory you got    //these lines will call the destructor for class Base    delete foo;    delete poo;} 


-me
constructors r used mainly for initializing the data members of a class to keep them in a consistent state. when u create an object of a class the constructor will immediately initialize the data members (private) of that object.

lets say that u have a class with no constructor to initialze data. wat will happen if the client of ur class tries to use the vlaues of ur object data(obviously through some public function).
Definitely the client will get garbage values. So constructors r used to keep the object data in a consistent state, in future u will see that it is absolutely necessary to initialze const and references in constructors as well as member data objects of a class using member initializwer list

Now lets come to the destructors....there is no such use of destructors unless we create dynamic objects(at run time) we need destructors usually to free the memory when the object is being destroyed. Destructors r called automatically when their life time comes to an end.

Moreover we cannot overload destructors but we can overload constructors. i.e bcoz destructors cannot accept any arguments

thats all my friend...hope it may be useful for u :-)
All constructors and deconstructors do is initialization and shutdown work for the object when it is created and/or destroyed. There are some half-truths in those 2 replies that should be cleared up.

First off, the constructor and destructor do not allocate the memory that an object itself requires, and constructors do not return pointers. That''s done by new and delete.

Secondly, constructors and deconstructors don''t necessarily have anything to do with dynamic memory allocation. In the following code:

#include <iostream>using namespace std;class Foo{  public:    Foo() { cout << "Foo()" << endl;    ~Foo() { cout << "~Foo()" << endl;};int main(int argc, char ** argv){  Foo f;  return 0;} 


The constructor and deconstructor are both called without any memory ever being dynamically allocated.
thanks for clearing that up for me, i was getting a little confused myself after a few of the replies, because i was thinkin about the new and delete things in order to allocate memory, and was kind of getting that confused with the constructors and destructors. So thank you for clearing that up for me, and i appreciate everyone help.

Dario
Advertisement
quote:
Original post by Palidine
class Base {protected:    int var1;    int var2;    int * myArray;public:    //basic constructor;    Base() {        var1 = 0;        var2 = 0;        myArray = NULL;    }    //more complex constructor    Base(int _var1, int var2, int arraySize) {        var1 = _var1;        var2 = _var2;        myArray = new int[arraySize];    }    //destructor    ~Base() {        //if myArray is not NULL        if (myArray)            delete[] myArray;    }};  


usage:

int main(void) {    //get a Base object with everything set to 0 or NULL    Base * foo = new Base();    //get a Base object with variables initialized    Base * poo = new Base(1,2,5);    //at end of fctn delete all the memory you got    //these lines will call the destructor for class Base    delete foo;    delete poo;}  


-me


You could also use Base() : var1(0), var2(0), //etc.





K I L A H M A N J A R O !



Wachar's Eternity <-<-<-<-<- Me own site!

This topic is closed to new replies.

Advertisement