-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcasting_operators.cpp
More file actions
65 lines (49 loc) · 969 Bytes
/
casting_operators.cpp
File metadata and controls
65 lines (49 loc) · 969 Bytes
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
#include <iostream>
#include <string>
struct A {
int value;
};
struct B {
int value;
// group 1
// ctors
B(int value): value(value) {}
B(std::string const& str): value(std::stoi(str)) {}
B(A const& a): value(a.value) {}
// group2
// assignment operators
B& operator=(int value) {
this->value = value;
return *this;
}
B& operator=(std::string const& str) {
this->value = std::stoi(str);
return *this;
}
B& operator=(const A& a) {
this->value = a.value;
return *this;
}
// group3
// casting operators
operator int() const { return value; }
operator std::string() const { return std::to_string(value); }
operator A() const { return A{value}; }
};
int main() {
A a {10};
using namespace std::literals::string_literals;
// group1
B b1 = a;
B b2 = 0;
B b3 = "0"s;
// group 2
b1 = a;
b2 = 0;
b3 = "0"s;
//group 3
a = b1;
int x = b2;
std::string s = b3;
return 0;
}