Ownership
Unique Ptr
std::unique_ptr gives one owner responsibility for an object.
unique ownership
A `std::unique_ptr` deletes its object automatically when the owner goes out of scope.
Unique Ptr
unique_ptr.cpp
Replay: real traced execution (multi-file project)
#include <iostream>
#include <memory>
#include <string>
class Note {
private:
std::string text;
public:
Note(std::string value) {
text = value;
}
std::string read() {
return text;
}
};
int main() {
std::string text = "alpha";
std::unique_ptr<Note> note = std::make_unique<Note>(text);
std::cout << "note=" << note->read() << std::endl;
std::cout << "owned=" << (note != nullptr) << std::endl;
return 0;
}
#include <iostream>
#include <memory>
#include <string>
class Note {
private:
std::string text;
public:
Note(std::string value) {
text = value;
}
std::string read() {
return text;
}
};
int main() {
std::string text = "beta";
std::unique_ptr<Note> note = std::make_unique<Note>(text);
std::cout << "note=" << note->read() << std::endl;
std::cout << "owned=" << (note != nullptr) << std::endl;
return 0;
}
#include <iostream>
#include <memory>
#include <string>
class Note {
private:
std::string text;
public:
Note(std::string value) {
text = value;
}
std::string read() {
return text;
}
};
int main() {
std::string text = "gamma";
std::unique_ptr<Note> note = std::make_unique<Note>(text);
std::cout << "note=" << note->read() << std::endl;
std::cout << "owned=" << (note != nullptr) << std::endl;
return 0;
}
text ← alpha
19int main() {20 std::string text→ alpha = "alpha"; //@text="beta", "gamma"2122 std::unique_ptr<Note> note = std::make_unique<Note>(textalpha);Note(std::string value)
9public:10 Note(std::string valuealpha) {11 text = valuealpha;12 }note ← (empty)
22std::unique_ptr<Note> note→ (empty) = std::make_unique<Note>(textalpha);2324std::cout << "note=" << note(empty)->read() << std::endl;25std::cout << "owned=" << (note != nullptr) << std::endl;std::string read()
14std::string read() {15 return textalpha;16}std::cout << "note=" << note->read() << std::endl;
24 std::cout << "note=" << note(empty)->read() << std::endl;25 std::cout << "owned=" << (note(empty) != nullptr) << std::endl;26 return 0;27}outputnote=alpha owned=1
text ← beta
19int main() {20 std::string text→ beta = "beta";2122 std::unique_ptr<Note> note = std::make_unique<Note>(textbeta);Note(std::string value)
9public:10 Note(std::string valuebeta) {11 text = valuebeta;12 }note ← (empty)
22std::unique_ptr<Note> note→ (empty) = std::make_unique<Note>(textbeta);2324std::cout << "note=" << note(empty)->read() << std::endl;25std::cout << "owned=" << (note != nullptr) << std::endl;std::string read()
14std::string read() {15 return textbeta;16}std::cout << "note=" << note->read() << std::endl;
24 std::cout << "note=" << note(empty)->read() << std::endl;25 std::cout << "owned=" << (note(empty) != nullptr) << std::endl;26 return 0;27}outputnote=beta owned=1
text ← gamma
19int main() {20 std::string text→ gamma = "gamma";2122 std::unique_ptr<Note> note = std::make_unique<Note>(textgamma);Note(std::string value)
9public:10 Note(std::string valuegamma) {11 text = valuegamma;12 }note ← (empty)
22std::unique_ptr<Note> note→ (empty) = std::make_unique<Note>(textgamma);2324std::cout << "note=" << note(empty)->read() << std::endl;25std::cout << "owned=" << (note != nullptr) << std::endl;std::string read()
14std::string read() {15 return textgamma;16}std::cout << "note=" << note->read() << std::endl;
24 std::cout << "note=" << note(empty)->read() << std::endl;25 std::cout << "owned=" << (note(empty) != nullptr) << std::endl;26 return 0;27}outputnote=gamma owned=1
One Owner
std::make_unique<Note>(text)creates oneNote.noteis the only owner of that object.note->read()uses the object while it is owned.- At the end of
main,notegoes out of scope and deletes the object.
note -> Note("alpha")
end of scope -> note releases Note
Exercise: unique_ptr.cpp
Create a unique_ptr owner for a small object, read through it, and print whether it is non-null