-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsmart-ptrs-unique-cpp11.cpp
More file actions
58 lines (43 loc) · 1.46 KB
/
smart-ptrs-unique-cpp11.cpp
File metadata and controls
58 lines (43 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#pragma clang diagnostic ignored "-Wunused-variable"
#pragma clang diagnostic ignored "-Wunused-value"
#pragma clang diagnostic ignored "-Wunused-function"
#define CATCH_CONFIG_MAIN // Tells Catch2 to provide a main()
#include "../catch/catch_amalgamated.hpp"
#include "utils.h"
using namespace std;
namespace {
//// unique_ptr — sole ownership | Rust: Box<T>
// - single owner, freed automatically when out of scope
// - zero overhead over raw pointer
//
// Key notes:
class MyClass {
int x;
public: MyClass(int x) : x(x) { }
int getX() { return this->x; };
};
//// factory pattern
class Base {};
class Derived : public Base {};
std::unique_ptr<Base> create() {
return std::make_unique<Derived>(); // ✓ polymorphism
}
TEST_CASE("uniq-1") {
//// create
auto up_int = std::make_unique<int>(42);
auto up = std::make_unique<MyClass>(42); // forwards to constructor
//// access
*up; // dereference
up.get(); // raw pointer — don't store, don't delete
up->getX(); // arrow access
REQUIRE(up->getX() == 42);
//// transfer ownership
auto p2 = std::move(up); // ✓ move: p now null
// auto p3 = up; // ❌ copy: copy ctor deleted — won't compile
//// release / reset
up_int.reset(); // destroy object, p = null
up_int.reset(new int(5)); // destroy old, own new
int* raw_ptr = up_int.release(); // give up ownership — YOU must delete raw
delete raw_ptr;
}
}