Constructors/ Destructors Run When?

Started by
4 comments, last by Bregma 16 years, 11 months ago
I've got two questions about constructors and destructor's: 1) When are constructor's run? Are they the first thing to run as an object is created or is something run before to create an instance of the class (or is that what the constructor is basically used for)? 2) Similarly, when are destructor's run? Does the class say, 'Ok I'm being "deleted", run destructor then deallocate myself", or is it more of the last thing run? Basically both questions are to find when exactly these two are run and if anything comes before or after them (depending on which one you're talking about).

=============================RhinoXNA - Easily start building 2D games in XNA!Projects

Advertisement
They are part of object creation and deletion, whether that be as part of operator new/delete, or when it's instantiated on the stack and falls out of scope.
Quote:Original post by DrEvil
They are part of object creation and deletion, whether that be as part of operator new/delete, or when it's instantiated on the stack and falls out of scope.


Yeah, I knew about them being creation and deletion and such, just curious if they're the definite starting and ending points of an object or if the computer itself runs that (as the programmers last bit of code for that particular object) and finishes up with some other background code that is automatically run when any type of object is deleted. I suppose it could depend on the OS.

=============================RhinoXNA - Easily start building 2D games in XNA!Projects

{  Object anInstance;  <-- calls Object::Object( ), default constructor  ....} < -- anInstance has gone out of scope, destructor is called---------void foo( Object o ) <-- by value{} <-- o goes out of scope here, the local copy calls destructorObject anInstance;foo( anInstance ) <-- may call copy constructor----------Object *po = new Object() <-- explicitly called constructordelete po;                <-- invokes destructor----------


Objects are constructed when they are declared.
The are destroyed when they go out of scope.

For pointers to object, they are constructed with call to new, destroyed with call to delete.



But then, there are lots of gotchas, with passing by reference, temporaries, exceptions, virtual destructors, multiple inheritance, assignment, etc....

Lots of gotchas.
Quote:Original post by programmermattc
is something run before to create an instance of the class (or is that what the constructor is basically used for)?

Constructors do not create objects. They merely initialize an already created object with meaningful values.
Quote:Original post by programmermattc
Yeah, I knew about them being creation and deletion and such, just curious if they're the definite starting and ending points of an object or if the computer itself runs that (as the programmers last bit of code for that particular object) and finishes up with some other background code that is automatically run when any type of object is deleted. I suppose it could depend on the OS.


It does not depend on the OS. The OS knows nothing about C++.

In C++, an object is considered constructed after the construcor returns successfully (note: not during the execution of the constructor). By the time the body of the constructor is executed, any contained or ancestral objects have already been successfully constructed.

An object is also not valid during the execution of its destructor: by the time the destructor is called, the object is laready considered destroyed.

The C++ runtime may peform additional operations before and after the constructor or destructor is run. For example, if an object is constructed on the free store, than any class operator new() or ::operator new() is invoked to allocate memory for the object. Conversely, an object constructed in automatic storage gets space reserved for it "on the stack". The opposite happens on destruction.

So, you're pretty much spot on in that the constructor is the definite ending point of the start of an object and the destructor the definite start of the end.

Ya ya?

--smw

Stephen M. Webb
Professional Free Software Developer

This topic is closed to new replies.

Advertisement