-
Notifications
You must be signed in to change notification settings - Fork 0
/
auto.cpp
69 lines (63 loc) · 1.65 KB
/
auto.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <iostream>
struct MyClass
{
MyClass() noexcept
{
std::cout << "MyClass default constructor" << std::endl;
}
MyClass(const MyClass& obj) noexcept
{
std::cout << "MyClass const copy-constructor" << std::endl;
}
MyClass(MyClass& obj) noexcept
{
std::cout << "MyClass copy-constructor" << std::endl;
}
MyClass(const MyClass&& obj) noexcept
{
std::cout << "MyClass const move-constructor" << std::endl;
}
MyClass(MyClass&& obj) noexcept
{
std::cout << "MyClass move-constructor" << std::endl;
}
~MyClass() noexcept
{
std::cout << "MyClass destructor" << std::endl;
}
MyClass& operator=(MyClass&) noexcept
{
std::cout << "MyClass operator=(MyClass&)" << std::endl;
return *this;
}
const MyClass& operator=(const MyClass&) const noexcept
{
std::cout << "MyClass operator=(const MyClass&)" << std::endl;
return *this;
}
};
static const MyClass getValue() noexcept { return { }; }
static const MyClass* getValuePtr() noexcept { return new MyClass { }; }
static const int* getValueIntPtr() noexcept { return new int { 5 }; }
int main()
{
const MyClass obj1;
// Calls MyClass(const MyClass& obj);
MyClass obj2 { obj1 };
const MyClass obj4 { };
// Calls MyClass(const MyClass&& obj);
MyClass obj3 { std::move(obj4) };
std::cout << "---" << std::endl;
auto value = getValue();
// The following two lines of code compiles fine
const auto* value2 = getValuePtr();
value2 = new MyClass { };
// But the following won't compile
// const auto value2 = getValuePtr();
// value2 = new MyClass { };
const auto* value3 = getValueIntPtr();
// Doesn't compile; even with 'auto* value3'
// *value3 = 10;
value3 = new int { 10 };
return 0;
}