944,047 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 526
  • C++ RSS
Oct 19th, 2009
0

Constructor and Destructor

Expand Post »
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?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
gokhanxyz is offline Offline
5 posts
since Oct 2009
Oct 19th, 2009
1
Re: Constructor and Destructor
Click to Expand / Collapse  Quote originally posted by gokhanxyz ...
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.
Last edited by tux4life; Oct 19th, 2009 at 4:13 pm.
Reputation Points: 2125
Solved Threads: 243
Postaholic
tux4life is offline Offline
2,105 posts
since Feb 2009
Oct 19th, 2009
0
Re: Constructor and Destructor
suppose;

C++ Syntax (Toggle Plain Text)
  1. class array{
  2.  
  3. private:
  4.  
  5. int number_of_elements;
  6. int *array_ptr;
  7.  
  8. public:
  9.  
  10. array(int x){
  11.  
  12. number_of_elements=x;
  13. array_ptr = new int[x];
  14.  
  15. }
  16.  
  17. ~array(){
  18.  
  19. delete [] array_ptr;
  20.  
  21. }
  22.  
  23. };
  24.  
  25. void main(void){
  26.  
  27. array new_array(10);
  28.  
  29. }

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?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
gokhanxyz is offline Offline
5 posts
since Oct 2009
Oct 19th, 2009
0
Re: Constructor and Destructor
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:
c++ Syntax (Toggle Plain Text)
  1. class Example
  2. {
  3. private:
  4. int x;
  5. double d;
  6. MyObj MA;
  7. MyObj MB;
  8. public:
  9. Example() : x(5),MA(4,5)
  10. {
  11. MB=MA;
  12. }
  13. };
  14. int main()
  15. { 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
Last edited by StuXYZ; Oct 19th, 2009 at 5:46 pm.
Reputation Points: 749
Solved Threads: 135
Practically a Master Poster
StuXYZ is offline Offline
660 posts
since Nov 2008
Oct 19th, 2009
0
Re: Constructor and Destructor
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?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
gokhanxyz is offline Offline
5 posts
since Oct 2009
Oct 19th, 2009
0
Re: Constructor and Destructor
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.
Reputation Points: 749
Solved Threads: 135
Practically a Master Poster
StuXYZ is offline Offline
660 posts
since Nov 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Creating a .txt file from a variable
Next Thread in C++ Forum Timeline: Fraction calculator





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC