回答編集履歴
4
追記
test
CHANGED
@@ -253,3 +253,95 @@
|
|
253
253
|
|
254
254
|
|
255
255
|
```
|
256
|
+
|
257
|
+
「追記」
|
258
|
+
|
259
|
+
コンストラクタで初期化するというのは↓のような事ですが?
|
260
|
+
|
261
|
+
```c++
|
262
|
+
|
263
|
+
usr ~/Project/test/teratail % c++ test.cpp
|
264
|
+
|
265
|
+
usr ~/Project/test/teratail % ./a.out
|
266
|
+
|
267
|
+
7
|
268
|
+
|
269
|
+
3
|
270
|
+
|
271
|
+
usr ~/Project/test/teratail % cat test.cpp
|
272
|
+
|
273
|
+
#include <iostream>
|
274
|
+
|
275
|
+
|
276
|
+
|
277
|
+
// function prototype
|
278
|
+
|
279
|
+
int sub(int a, int b);
|
280
|
+
|
281
|
+
int add(int a, int b);
|
282
|
+
|
283
|
+
|
284
|
+
|
285
|
+
class Test
|
286
|
+
|
287
|
+
{
|
288
|
+
|
289
|
+
|
290
|
+
|
291
|
+
int (*func)(int, int);
|
292
|
+
|
293
|
+
|
294
|
+
|
295
|
+
public:
|
296
|
+
|
297
|
+
Test(int (*f)(int, int)) : func(f) {}
|
298
|
+
|
299
|
+
int exec(int a, int b) { return (*func)(a, b); }
|
300
|
+
|
301
|
+
};
|
302
|
+
|
303
|
+
|
304
|
+
|
305
|
+
int add(int a, int b)
|
306
|
+
|
307
|
+
{
|
308
|
+
|
309
|
+
return a + b;
|
310
|
+
|
311
|
+
}
|
312
|
+
|
313
|
+
|
314
|
+
|
315
|
+
int sub(int a, int b)
|
316
|
+
|
317
|
+
{
|
318
|
+
|
319
|
+
return a - b;
|
320
|
+
|
321
|
+
}
|
322
|
+
|
323
|
+
|
324
|
+
|
325
|
+
int main(void)
|
326
|
+
|
327
|
+
{
|
328
|
+
|
329
|
+
Test ta(add);
|
330
|
+
|
331
|
+
Test ts(sub);
|
332
|
+
|
333
|
+
|
334
|
+
|
335
|
+
std::cout << ta.exec(5, 2) << std::endl;
|
336
|
+
|
337
|
+
std::cout << ts.exec(5, 2) << std::endl;
|
338
|
+
|
339
|
+
|
340
|
+
|
341
|
+
return 0;
|
342
|
+
|
343
|
+
}
|
344
|
+
|
345
|
+
usr ~/Project/test/teratail %
|
346
|
+
|
347
|
+
```
|
3
ソース追記
test
CHANGED
@@ -141,3 +141,115 @@
|
|
141
141
|
usr ~/Project/test/teratail %
|
142
142
|
|
143
143
|
```
|
144
|
+
|
145
|
+
```c++
|
146
|
+
|
147
|
+
#include <iostream>
|
148
|
+
|
149
|
+
//
|
150
|
+
|
151
|
+
using namespace std;
|
152
|
+
|
153
|
+
|
154
|
+
|
155
|
+
//using FUNC_VO = void (*)();
|
156
|
+
|
157
|
+
// using FUNC_IN = int (*)(int, int);
|
158
|
+
|
159
|
+
|
160
|
+
|
161
|
+
class test
|
162
|
+
|
163
|
+
{
|
164
|
+
|
165
|
+
public:
|
166
|
+
|
167
|
+
int add(int a, int b) { return a + b; }
|
168
|
+
|
169
|
+
|
170
|
+
|
171
|
+
void f();
|
172
|
+
|
173
|
+
|
174
|
+
|
175
|
+
void (*fx)();
|
176
|
+
|
177
|
+
};
|
178
|
+
|
179
|
+
|
180
|
+
|
181
|
+
void test::f()
|
182
|
+
|
183
|
+
{
|
184
|
+
|
185
|
+
int (test::*fp)(int a, int b) = &test::add;
|
186
|
+
|
187
|
+
int x = (this->*fp)(1, 2);
|
188
|
+
|
189
|
+
printf("%d\n", x);
|
190
|
+
|
191
|
+
}
|
192
|
+
|
193
|
+
|
194
|
+
|
195
|
+
static void f(void)
|
196
|
+
|
197
|
+
{
|
198
|
+
|
199
|
+
printf("関数ポインタ\n");
|
200
|
+
|
201
|
+
}
|
202
|
+
|
203
|
+
|
204
|
+
|
205
|
+
static void (*f2(int (*fp)(int, int)))()
|
206
|
+
|
207
|
+
{
|
208
|
+
|
209
|
+
int a;
|
210
|
+
|
211
|
+
int b;
|
212
|
+
|
213
|
+
scanf("%d", &a);
|
214
|
+
|
215
|
+
scanf("%d", &b);
|
216
|
+
|
217
|
+
|
218
|
+
|
219
|
+
printf("%d\n", (*fp)(a, b));
|
220
|
+
|
221
|
+
//
|
222
|
+
|
223
|
+
return f;
|
224
|
+
|
225
|
+
}
|
226
|
+
|
227
|
+
|
228
|
+
|
229
|
+
static int adds(int a, int b)
|
230
|
+
|
231
|
+
{
|
232
|
+
|
233
|
+
return a + b;
|
234
|
+
|
235
|
+
}
|
236
|
+
|
237
|
+
|
238
|
+
|
239
|
+
int main()
|
240
|
+
|
241
|
+
{
|
242
|
+
|
243
|
+
void (*sample)() = f2(adds);
|
244
|
+
|
245
|
+
(*sample)();
|
246
|
+
|
247
|
+
|
248
|
+
|
249
|
+
return 0;
|
250
|
+
|
251
|
+
}
|
252
|
+
|
253
|
+
|
254
|
+
|
255
|
+
```
|
2
ソース修正
test
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
```c++
|
4
4
|
|
5
|
-
#
|
5
|
+
#include <iostream>
|
6
6
|
|
7
7
|
//
|
8
8
|
|
1
ソース修正
test
CHANGED
@@ -2,17 +2,15 @@
|
|
2
2
|
|
3
3
|
```c++
|
4
4
|
|
5
|
-
#include <iostream>
|
5
|
+
##include <iostream>
|
6
6
|
|
7
|
-
//
|
7
|
+
//
|
8
|
-
|
9
|
-
#include <math.h>
|
10
8
|
|
11
9
|
using namespace std;
|
12
10
|
|
13
11
|
|
14
12
|
|
15
|
-
using FUNC_
|
13
|
+
using FUNC_VO = void (*)();
|
16
14
|
|
17
15
|
using FUNC_IN = int (*)(int, int);
|
18
16
|
|
@@ -52,17 +50,17 @@
|
|
52
50
|
|
53
51
|
|
54
52
|
|
55
|
-
void f(void)
|
53
|
+
static void f(void)
|
56
54
|
|
57
55
|
{
|
58
56
|
|
59
|
-
printf("関数ポインタ");
|
57
|
+
printf("関数ポインタ\n");
|
60
58
|
|
61
59
|
}
|
62
60
|
|
63
61
|
|
64
62
|
|
65
|
-
FUNC_
|
63
|
+
static FUNC_VO f2(FUNC_IN fp)
|
66
64
|
|
67
65
|
{
|
68
66
|
|
@@ -84,21 +82,9 @@
|
|
84
82
|
|
85
83
|
}
|
86
84
|
|
87
|
-
/*
|
88
|
-
|
89
|
-
void (* fr())
|
90
|
-
|
91
|
-
{
|
92
|
-
|
93
|
-
return f;
|
94
|
-
|
95
|
-
}
|
96
|
-
|
97
|
-
*/
|
98
85
|
|
99
86
|
|
100
|
-
|
101
|
-
int adds(int a, int b)
|
87
|
+
static int adds(int a, int b)
|
102
88
|
|
103
89
|
{
|
104
90
|
|
@@ -107,18 +93,6 @@
|
|
107
93
|
}
|
108
94
|
|
109
95
|
|
110
|
-
|
111
|
-
/*
|
112
|
-
|
113
|
-
xx t()
|
114
|
-
|
115
|
-
{
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
}
|
120
|
-
|
121
|
-
*/
|
122
96
|
|
123
97
|
int main()
|
124
98
|
|
@@ -130,7 +104,9 @@
|
|
130
104
|
|
131
105
|
// void (*x)() = f2(adds);
|
132
106
|
|
133
|
-
FUNC_
|
107
|
+
FUNC_VO sample = f2(adds);
|
108
|
+
|
109
|
+
(*sample)();
|
134
110
|
|
135
111
|
|
136
112
|
|
@@ -140,22 +116,28 @@
|
|
140
116
|
|
141
117
|
|
142
118
|
|
119
|
+
|
120
|
+
|
121
|
+
|
122
|
+
|
143
123
|
```
|
144
124
|
|
145
125
|
結果
|
146
126
|
|
147
127
|
```text
|
148
128
|
|
129
|
+
usr ~/Project/test/teratail % c++ t182725.cpp
|
130
|
+
|
149
|
-
usr ~/Project/test/teratail % ./a.out
|
131
|
+
usr ~/Project/test/teratail % ./a.out
|
132
|
+
|
133
|
+
2
|
150
134
|
|
151
135
|
3
|
152
136
|
|
153
|
-
|
137
|
+
5
|
154
138
|
|
155
|
-
|
139
|
+
関数ポインタ
|
156
140
|
|
157
141
|
usr ~/Project/test/teratail %
|
158
142
|
|
159
|
-
|
160
|
-
|
161
143
|
```
|