0
Destructors C++
Well, there's something I don't clearly understand : What does exactly the destructor do to the instance of a class? I mean, it's called right after the Object is created, but the Object's still usable. So what does it exactly destroy?
1 Answer
+ 4
Destructors are called when objects are destroyed. For example:
class A{
~A(){cout << "Destroyed"}
}
When you create an object:
int main(){
A a;
}
The program will create the object and destroy it at the end of the program. When it is destroyed, it will output "Destroyed".
I think you may have been confused with constructors, which are those run when an object is created. For example:
class B{
B(){cout << "Constructed";}
}
When you create an instance of B, the constructor will be called, outputting "Constructed" and then destroyed promptly afterwards at the end of the program.
Hope this helped! đ