example2_exception_safety.cpp

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <memory>

class MyClass {
public:
    MyClass(int value) : value_(value) {
        std::cout << "MyClass constructed with value: " << value_ << std::endl;
    }
    ~MyClass() {
        std::cout << "MyClass destructed" << std::endl;
    }
    int getValue() const { return value_; }
private:
    int value_;
};

int main() {
    auto ptr = std::make_unique<MyClass>(42);
    std::cout << "Value: " << ptr->getValue() << std::endl;
    return 0;
}
Back to std_make_unique