Question:
constructor and destructor?
Cloud
2007-03-15 10:46:43 UTC
I am still learning C++.

Why do we need constructor, default constructor and copy constructor in classes?? On the contrary, Why needs destructor?
Six answers:
phillipamartin
2007-03-15 12:00:34 UTC
Constructors are used to setup and initialize objects. In C++ specifically they're usually used to allocate and initialize memory, set default values for variables, etc.

Copy constructors are needed to allow one instance of an object to be cloned into a second instance of an object, so that memory references aren't shared, but completely copied.

Destructors are the opposite of a constructor, they deallocate memory, to prevent "memory leaks" when an object goes out of scope or is explicitly destroyed.
Christina
2007-03-15 11:14:44 UTC
Well a constructor and destructor are used in object creation and termination. An object resides in the Heap and is pointed (or referenced) to via a variable residing on the Stack or the Heap. The constructor is what allows for the creation of a new object on the Heap. It can be used to initialize instance variables or do nothing but create an object. In Java, there is a default constructor for every object even if you don't specify one which allows for the use of the "new" keyword creating an object. The destructor is the opposite and removes the object from memory, though does not necessarily remove the reference.
gaurav s
2007-03-15 11:36:46 UTC
Well a constructor is needed to initialize the objects when they are created, it gets automatically called so u are relieved of explicitly initializing the objects.

On the other hand the destructor does the opposite of what constructor does it frees the object when the object goes out of scope.

Is there anything else you need to know pl tell
Pfo
2007-03-15 12:01:59 UTC
This goes back to the days of C, when there were no classes people would use structs to group data. Often, they needed to initialize structs and create initialization functions for those structs. Also, when deallocating those structs they needed to finalize some struct resources. So when C++ came along, it was logical to make this behavior built into a class so that you didn't accidentally forget to call an initialization or finalization method. Basically, class creation and destruction could fit a "fire and forget" model.
privett
2016-12-02 06:27:34 UTC
I idea rock replaced into once useless in the Eighties, I hated Hair Bands and Punk and Disco. The 90s proved me incorrect after I heard Nirvana, Pearl Jam, Stone Temple Pilots and Sound backyard. it style of sounds like rock may be demise once more beneficial now that those bands are lengthy gone in spite of the undeniable fact that it basically relies upon on what you adore. It you don't like the clean stuff on the time then that's going to look like rock is demise. Of route music has to regulate or we may all be unwell of a similar historic ingredient. that's harder this contemporary day to be a supergroup pondering no individual can healthful record sales of the quicker. persons this cutting-aspect basically receive the song they favor. Alot of the time that's in undemanding words one song and not in any respect the finished album. in the 70s i'd purchase an album no longer for one song in spite of the undeniable fact that for all of them. also for the cover artwork of the album and what it had to assert. youthful persons at present would not have the feeling of possessing a precise actual series. they basically positioned song correct right into a computer and thats it. Rock will easily no longer die considering there'll in any respect circumstances be that youthful musician in the promote it truly is sturdy adequate to come back up with some thing new yet use the impacts of the previous to do some thing like Kurt Cobain did.
anonymous
2007-03-15 10:55:29 UTC
constructors instantiate the class into an object in memory. A deconstructor removes the references to that object.



EDIT: My explanation of deconstructor implies something wrong, it doesn't remove the references from other objects, it decontructs the object/removes it from memory


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...