Constructor | Deconstructor

Jun 14, 2012 at 3:01pm
Hi people,

I am doing the tutorial from this website, and I did the part of classes, but there is one thing that I don't understand: what is the use of constructors and deconstructors? Could anyone explain that?
Thanks in advance!
Jun 14, 2012 at 3:03pm
Constructors and destructors will implicitly run when and object is created or deleted respectively.

You'd normally use constructors for initialising class values.

You'd normally use destructors for tidying up. Maybe deleting assigned memory and such.
Jun 14, 2012 at 5:47pm
So you use the constructor to assign values to private/protected variables?
Jun 14, 2012 at 6:14pm
You can use constructors to set the state of your object. Depending on the situation in your program, you may need one of a dozen different states. So you can overload constructors to allow this to happen.

tl;dr
Constructors are used to initialize members, while destructors are used typically to free up dynamically allocated memory.
Jun 14, 2012 at 6:18pm
Thanks! I'm starting to get it :) but could you give an example of a state?
Topic archived. No new replies allowed.