Using smart pointers to manage memory automatically.
cppCopy code#include <iostream>
#include <memory>
class MyClass {
public:
MyClass() { std::cout << "MyClass constructor" << std::endl; }
~MyClass() { std::cout << "MyClass destructor" << std::endl; }
};
int main() {
std::unique_ptr<MyClass> ptr = std::make_unique<MyClass>();
// No need to manually delete; the destructor will be called automatically
return 0;
}
Leave a Reply