Destructors C++ | Sololearn: Learn to code for FREE!
Nouvelle formation ! Tous les codeurs devraient apprendre l'IA générative !
Essayez une leçon gratuite

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?

11th Dec 2017, 11:41 AM
Lucas S
Lucas S - avatar
1 Réponse
+ 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! ūüėČ
11th Dec 2017, 12:14 PM
blackcat1111 - avatar