add a simple example to unique_ptr.cpp

master
neingeist 10 years ago
parent 94de91cd85
commit 6463aab35c

@ -3,12 +3,26 @@
#include <memory>
#include <vector>
int main(void) {
void simple() {
// From Wikipedia.
std::unique_ptr<int> p1(new int(5));
// Compile error - can't copy p1!
// std::unique_ptr<int> p2 = p1;
assert(p1 != NULL);
// Transfers ownership. p3 now owns the memory and p1 is rendered invalid:
std::unique_ptr<int> p3 = std::move(p1);
assert(p1 == NULL && p3 != NULL);
}
void container() {
std::vector<std::unique_ptr<int>> v;
std::unique_ptr<int> q(new int(42));
assert(q != NULL);
// v.push_back(q);
// v.push_back(q); <- Compile error
v.push_back(std::move(q));
assert(q == NULL);
@ -16,3 +30,8 @@ int main(void) {
std::cout << *e << std::endl;
}
}
int main(void) {
simple();
container();
}

Loading…
Cancel
Save