Hello...I was just wondering ever since I started coding (started with Java and now with C++), I've never for the life of me figured out what constructors and deconstructors are fundamentally. Classes are the pinnacle of OO programming, and yet this entire time I've learned them just from memorization rather than fundamentally understanding what they are.
Usually when I look at code, I can explain it in English despite how convoluted it might be it still makes sense and more-over the order makes sense, but I can't seem to grasp the point of constructors and deconstructors overall. Can anybody here explain them please =s?
Do not call them "deconstructors". Most people call them "destructors".
So what are they? They are special functions that get called automatically when an object is created (constructor) and again when an object is destroyed (destructor).
They are "special" functions in that they have no return value. In addition the destructor takes no parameters, ever.
They have the purpose of initializing the "contents" of the object (constructor) or to release resources when the object owning them dies (destructor).
#include <iostream>
class Test
{
private:
int *pi;
// these below were "removed" to keep the class simple
Test(const Test &); // copy constructor
Test & operator = (const Test &); // copy operator
public:
Test(): pi(newint[100])
{
std::clog << "Constructor Test::Test() for object at address " << this << '\n';
std::clog << "Memory was allocated.\n";
}
~Test()
{
delete[] pi;
std::clog << "Destructor Test::~Test() for object at address " << this << '\n';
std::clog << "Memory was deallocated.\n";
}
};
int main()
{
Test t1, t2, t3;
}
Constructor Test::Test() for object at address 0x28ff1c
Memory was allocated.
Constructor Test::Test() for object at address 0x28ff18
Memory was allocated.
Constructor Test::Test() for object at address 0x28ff14
Memory was allocated.
Destructor Test::~Test() for object at address 0x28ff14
Memory was deallocated.
Destructor Test::~Test() for object at address 0x28ff18
Memory was deallocated.
Destructor Test::~Test() for object at address 0x28ff1c
Memory was deallocated.
Thanks, I do understand construct/destructors a little more now, I still can't seem to put the stuff in English in my head though XD the same way I don't fully understand why functions have to be called before the main function if they're not used above the main function, but I can at least appreciate the constructor a little more in how it essentially acts like a function except it's called when the class is.
I do have one final question though, how does one call the destructor and does the code in the destructor activate upon any instance of the class being...destructed XD I read somewhere that there are several scenarios in which the class is destructed or w/e, so would the code activate upon that?
Which makes me wonder- why are they needed. What if the code has no use for the class to do anything when being initially called or to do anything when ending, are you stuck beign forced into using these two empty functions because C++ is built that way? I've seen classes without a destructor before, is this possible? Is it impossible for the code to execute without a constructor and impossible for the class to "stop" without a destructor, despite having no code in those two functions?
And this is where I realized I asked more than "one final question" hehe, sorry.
I do have one final question though, how does one call the destructor and does the code in the destructor activate upon any instance of the class being...destructed XD I read somewhere that there are several scenarios in which the class is destructed or w/e, so would the code activate upon that?
The destructor for an object is called automatically when that object goes out of scope.
1 2 3 4 5 6 7
int main()
{
{
Object o;
}
// <--- here the object is out of scope, and was destroyed by now
}
You can call the destructor manually but you should never do that (at least not until you become an expert in C++).
The main reason why you shouldn't call the destructor manually is because it will be called automatically a second time, and this may lead to nasty problems.
#include <iostream>
class Whine
{
public:
~Whine()
{
std::clog << this << " was destroyed!\n";
}
};
int main()
{
{
Whine w;
w.~Whine();
}
}
0x28ff1f was destroyed!
0x28ff1f was destroyed!
Which makes me wonder- why are they needed.
I believe I already explained that.
What if the code has no use for the class to do anything when being initially called or to do anything when ending, are you stuck beign forced into using these two empty functions because C++ is built that way? I've seen classes without a destructor before, is this possible?
If you don't write any constructors (you can write more than one) then C++ will implicitly define an "empty" constructor. Same thing happens in the case of the destructor.
Oh ok so not writing one automatically generates an empty one alright =) thanks for that! Seriously thanks for this extrapolation.
Multiple constructors O_o I'm not even sure as to how that works as I assume the variables are declared as part of the class, right? ...I guess I'll get there eventually XD thanks so much.
Multiple constructors O_o I'm not even sure as to how that works as I assume the variables are declared as part of the class, right?
You provide multiple constructors for convenience.
Here's an incomplete example, illustrating the principle:
1 2 3 4 5 6 7 8 9 10
// suppose we have a 3D point class named Point3d
// and that it holds x, y, z coordinates
Point3d p1; // default constructor, x y z are set 0 (for example)
Point3d p2(1, 2, 3); // a constructor that sets x=1 y=2 z=3
int xyz[] = {4, 5, 6};
Point3d p3(xyz); // a constructor that uses the xyz array
So it's all about convenience. Again, constructors are merely functions that initialize (give values to) the member data of the class.