Smart Pointers

Using smart pointers to manage memory automatically.

cppCopy code#include <iostream>
#include <memory>

class MyClass {
public:
MyClass() { std::cout &lt;&lt; "MyClass constructor" &lt;&lt; std::endl; }
~MyClass() { std::cout &lt;&lt; "MyClass destructor" &lt;&lt; std::endl; }
}; int main() {
std::unique_ptr&lt;MyClass&gt; ptr = std::make_unique&lt;MyClass&gt;();
// No need to manually delete; the destructor will be called automatically
return 0;
}

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *