Search⌘ K

- Example

Explore how C++ destructors are called according to the scope of objects. Understand the lifecycle of objects declared globally, locally, and dynamically, and learn to manage memory safely by using delete to invoke destructors timely. This lesson helps you grasp object lifetime and resource cleanup fundamentals essential for writing robust C++ code.

We'll cover the following...

Various scopes #

C++
#include <iostream>
class HelloGoodBye{
public:
HelloGoodBye(int i):numb(i){
std::cout << "Hello from " << numb << ": "<< std::endl;
}
~HelloGoodBye();
private:
int numb;
};
HelloGoodBye::~HelloGoodBye(){
std::cout << "Good Bye from : " << numb << std::endl;
}
void func(){
HelloGoodBye helloGoodBye(5);
}
HelloGoodBye helloGoodBye(1);
int main(){
std::cout << std::endl;
HelloGoodBye helloGoodBye(2);
std::cout << std::endl;
HelloGoodBye* helloGoodByePtr = new HelloGoodBye(3);
std::cout << std::endl;
{
HelloGoodBye helloGoodBye(4);
}
std::cout << std::endl;
delete helloGoodByePtr;
func();
std::cout << "--------- End of main ----------" << std::endl;
}

Explanation #

The example shows how the destructors of different HelloGoodbye ...