Sign in to follow this  
programmermattc

Constructors/ Destructors Run When?

Recommended Posts

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).

Share this post


Link to post
Share on other sites
DrEvil    1148
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Antheus    2409


{
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 destructor

Object anInstance;
foo( anInstance ) <-- may call copy constructor

----------

Object *po = new Object() <-- explicitly called constructor
delete 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.

Share this post


Link to post
Share on other sites
DevFred    840
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.

Share this post


Link to post
Share on other sites
Bregma    9214
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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this