forked from MicrosoftDocs/cpp-docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmemory-functions_1.cpp
45 lines (35 loc) · 1.18 KB
/
memory-functions_1.cpp
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
class Animal
{
private:
std::wstring genus;
std::wstring species;
int age;
double weight;
public:
Animal(const wstring&, const wstring&, int, double){/*...*/ }
Animal(){}
};
void MakeAnimals()
{
// Use the Animal default constructor.
unique_ptr<Animal> p1 = make_unique<Animal>();
// Use the constructor that matches these arguments
auto p2 = make_unique<Animal>(L"Felis", L"Catus", 12, 16.5);
// Create a unique_ptr to an array of 5 Animals
unique_ptr<Animal[]> p3 = make_unique<Animal[]>(5);
// Initialize the elements
p3[0] = Animal(L"Rattus", L"norvegicus", 3, 2.1);
p3[1] = Animal(L"Corynorhinus", L"townsendii", 4, 1.08);
// auto p4 = p2; //C2280
vector<unique_ptr<Animal>> vec;
// vec.push_back(p2); //C2280
// vector<unique_ptr<Animal>> vec2 = vec; // C2280
// OK. p2 no longer points to anything
vec.push_back(std::move(p2));
// unique_ptr overloads operator bool
wcout << boolalpha << (p2 == false) << endl; // Prints "true"
// OK but now you have two pointers to the same memory location
Animal* pAnimal = p2.get();
// OK. p2 no longer points to anything
Animal* p5 = p2.release();
}