Does the constructor create an instance of a class? or is it a member function of a class that is invoked automatically after the creation of the object is compeleted?

same question about the destructor;

does it deallocate the object itself or does it have the responsibility of making the extra clean up for the memory that was allocated for the use of the object?

i mean; are the creation of an object and constructor, deallocating the object itself and destructor related?

Recommended Answers

All 5 Replies

Does the constructor create an instance of a class? or is it a member function of a class that is invoked automatically after the creation of the object is compeleted?

same question about the destructor;

does it deallocate the object itself or does it have the responsibility of making the extra clean up for the memory that was allocated for the use of the object?

i mean; are the creation of an object and constructor, deallocating the object itself and destructor related?

The constructor is called when (= at the time) an object of a class is created.
The object's destructor is called at the time the object is destructed.
When you've manually allocated memory inside the object then a default destructor won't automatically free it for you, unless you explicitly write your destructor to deallocate the memory you had previously allocated.

suppose;

class array{

private:

        int number_of_elements;
        int *array_ptr;

public: 

         array(int x){

                   number_of_elements=x;
                   array_ptr = new int[x];
       
         }

        ~array(){

                   delete [] array_ptr;

         }

};

void main(void){

      array new_array(10);

}

regarding to this code, as far as i understand, during the execution of main an instance of class array is instantiated, after this instanstiation process the constructor is executed on the new_array.

and after execution of main, at the end i mean, since now new_array is at the out of its scope it should be destroyed, but this destruction process is before a call to the destructor of the new_array.

am i right or wrong?

Let use be a little careful here about the sequence of events.
First off, if you examine the assembler/machine code that is 100% exactly what is going on. However, (a) that may mean that your compiler is being "smart" (b) the standard says what the effect should be. So now we will discuss what actually should happen:

So consider your example. First if the code is written as you have written it.
(a) The object is created
(b) then the code in { } below the constructor is called
(c) the delete operator is called
(d) the program exits.

Now let us write a better example:

class Example
{  
    private: 
        int x;
        double d;
        MyObj MA;
        MyObj MB;
   public:
       Example() : x(5),MA(4,5)
         {
            MB=MA;
         }
};
int main()
 {  Example A; }

Now what happens (assuming MyObj is definded elsewhere).

(a) x is initialized with the value 5.

(b) MA is constructed (calling the constructor with two values 4 and 5.

(c) MB is constructed (calling the constructor with no value e.g MB() (d) Then the assignment operator (=) is called on the previously constructed MB e.g. MB.operator=(MA); (e) the destructor for MB is called

(f) the destructor for MA is called

(g) memory for object A is marked free

e,f,g are the effect of the default destructor begin called.

Does that help / make it clear ??

It is maybe worth playing with the class but add destructor,constructors, copy constructors and assignment operators that printout their name etc.
print out in

after we call a destructor of an object explicitly, we can still reach the data members of the object. so does not this mean that destructor does not destroy the object as it name implies? also by the aid of this experiment can it be said that the setting the source of memory for the object itself free is related with destructor but they are dinstinct?

Calling the destructor normally implies that the memory is listed as free, i.e. you can quickly find that the memory is corrupted with something else.

Try valgind to get a detailed picture of deleted and valid memory , as well as gdb/ddd to look at the disassembly.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.