回答編集履歴

1

追記

2018/02/01 06:59

投稿

episteme
episteme

スコア16614

test CHANGED
@@ -87,3 +87,91 @@
87
87
  */
88
88
 
89
89
  ```
90
+
91
+
92
+
93
+ [追記] こゆこと?
94
+
95
+
96
+
97
+ ```C++
98
+
99
+ #include <iostream>
100
+
101
+
102
+
103
+ class Operation {
104
+
105
+ int last_;
106
+
107
+ public:
108
+
109
+ int add(int x, int y) { return last_ = x+y; }
110
+
111
+ int sub(int x, int y) { return last_ = x-y; }
112
+
113
+ int mul(int x, int y) { return last_ = x*y; }
114
+
115
+ int last() const { return last_; }
116
+
117
+ };
118
+
119
+
120
+
121
+ class Foo {
122
+
123
+ Operation* opr_;
124
+
125
+ public:
126
+
127
+ void set(Operation* opr) {
128
+
129
+ opr_ = opr;
130
+
131
+ }
132
+
133
+ int exec_add(int x, int y) {
134
+
135
+ return opr_->add(x, y);
136
+
137
+ }
138
+
139
+ };
140
+
141
+
142
+
143
+ int main() {
144
+
145
+ using namespace std;
146
+
147
+
148
+
149
+ Operation op;
150
+
151
+ Foo foo;
152
+
153
+
154
+
155
+ foo.set(&op);
156
+
157
+ cout << "1+2= " << foo.exec_add(1,2) << endl;
158
+
159
+ cout << "2+3= " << foo.exec_add(2,3) << endl;
160
+
161
+ cout << "last result = " << op.last() << endl;
162
+
163
+ }
164
+
165
+
166
+
167
+ /* 実行結果
168
+
169
+ 1+2= 3
170
+
171
+ 2+3= 5
172
+
173
+ last result = 5
174
+
175
+ */
176
+
177
+ ```