回答編集履歴

1

追記

2021/07/03 10:01

投稿

episteme
episteme

スコア16612

test CHANGED
@@ -43,3 +43,73 @@
43
43
  }
44
44
 
45
45
  ```
46
+
47
+
48
+
49
+ [追記」相互変換? できますとも。
50
+
51
+ ```C++
52
+
53
+ #include <iostream>
54
+
55
+
56
+
57
+ class bar;
58
+
59
+
60
+
61
+ class foo {
62
+
63
+ public:
64
+
65
+ int n;
66
+
67
+ foo(int v=0) : n(v) {}
68
+
69
+ void print() { std::cout << "foo(" << n << ")\n"; }
70
+
71
+ operator bar();
72
+
73
+ };
74
+
75
+
76
+
77
+ class bar {
78
+
79
+ public:
80
+
81
+ bar(int v=0) : n(v) {}
82
+
83
+ int n;
84
+
85
+ void print() { std::cout << "bar(" << n << ")\n"; }
86
+
87
+ operator foo();
88
+
89
+ };
90
+
91
+
92
+
93
+ bar::operator foo() { foo tmp; tmp.n = n; return tmp; }
94
+
95
+ foo::operator bar() { bar tmp; tmp.n = n; return tmp; }
96
+
97
+
98
+
99
+ int main() {
100
+
101
+ foo f0(123);
102
+
103
+ foo f1;
104
+
105
+ bar b0(456);
106
+
107
+ bar b1;
108
+
109
+ f1 = b0; f1.print();
110
+
111
+ b1 = f0; b1.print();
112
+
113
+ }
114
+
115
+ ```