質問編集履歴
3
自分の知りたい回答が得られるよう質問タイトルと内容を変更しました。
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
c++ クラスの関数ポインターの
|
1
|
+
c++ クラスのメンバの関数ポインターに関数のアドレスを入れる方法
|
test
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
クラスのメ
|
1
|
+
クラスのメンバ変数として関数ポインターを用意してそこにそのクラスの関数のポインターを入れたりクラスの外の関数のポインターを設定する方法が知りたいです。void(*scp)();// にsample::cfを設定する方法が知りたいのですが教えてくれますでしょうか?
|
2
2
|
|
3
3
|
|
4
4
|
|
@@ -6,11 +6,27 @@
|
|
6
6
|
|
7
7
|
#include <iostream>
|
8
8
|
|
9
|
-
|
9
|
+
#include "conio.h"
|
10
10
|
|
11
11
|
#include <math.h>
|
12
12
|
|
13
13
|
using namespace std;
|
14
|
+
|
15
|
+
|
16
|
+
|
17
|
+
|
18
|
+
|
19
|
+
|
20
|
+
|
21
|
+
|
22
|
+
|
23
|
+
void f()
|
24
|
+
|
25
|
+
{
|
26
|
+
|
27
|
+
printf("関数ポインター");
|
28
|
+
|
29
|
+
}
|
14
30
|
|
15
31
|
|
16
32
|
|
@@ -22,44 +38,6 @@
|
|
22
38
|
|
23
39
|
}
|
24
40
|
|
25
|
-
|
26
|
-
|
27
|
-
void f()
|
28
|
-
|
29
|
-
{
|
30
|
-
|
31
|
-
printf("関数ポインター");
|
32
|
-
|
33
|
-
}
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
//引数なし戻り値になし関数ポインタを返す関数
|
38
|
-
|
39
|
-
void (*fp())()
|
40
|
-
|
41
|
-
{
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
return f;
|
46
|
-
|
47
|
-
}
|
48
|
-
|
49
|
-
//戻り値ありで仮引数がある関数ポインタを返す関数
|
50
|
-
|
51
|
-
int (*fp2())(int a,int b)
|
52
|
-
|
53
|
-
{
|
54
|
-
|
55
|
-
return add;
|
56
|
-
|
57
|
-
}
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
41
|
|
64
42
|
|
65
43
|
class sample
|
@@ -68,41 +46,31 @@
|
|
68
46
|
|
69
47
|
public:
|
70
48
|
|
71
|
-
int cx = 0;
|
72
|
-
|
73
|
-
int cy = 0;
|
74
49
|
|
75
50
|
|
51
|
+
int cx;
|
76
52
|
|
77
|
-
void f
|
53
|
+
void cf()
|
78
54
|
|
79
55
|
{
|
80
56
|
|
81
|
-
printf("
|
57
|
+
printf("関数ポインター");
|
82
|
-
|
83
|
-
}
|
84
|
-
|
85
|
-
int fs(int x)const
|
86
|
-
|
87
|
-
{
|
88
|
-
|
89
|
-
printf("samlpeクラス");
|
90
|
-
|
91
|
-
return x;
|
92
58
|
|
93
59
|
}
|
94
60
|
|
95
61
|
|
96
62
|
|
97
|
-
|
63
|
+
int c_add(int a, int b)
|
98
64
|
|
99
65
|
{
|
100
66
|
|
101
|
-
|
67
|
+
return a + b;
|
102
|
-
|
103
|
-
cout << cy << endl;
|
104
68
|
|
105
69
|
}
|
70
|
+
|
71
|
+
|
72
|
+
|
73
|
+
void(*scp)();//
|
106
74
|
|
107
75
|
};
|
108
76
|
|
@@ -112,57 +80,37 @@
|
|
112
80
|
|
113
81
|
{
|
114
82
|
|
115
|
-
sample sa;
|
83
|
+
sample sa,sa2;
|
116
84
|
|
117
|
-
sample sa
|
85
|
+
sample *sp = new sample();
|
118
86
|
|
119
87
|
|
120
88
|
|
121
|
-
i
|
89
|
+
void(*fp)() = f;
|
122
90
|
|
123
|
-
p =
|
91
|
+
int(*fp2)(int a, int b) = add;
|
124
92
|
|
125
|
-
|
93
|
+
//(*fp)();
|
126
94
|
|
127
|
-
|
95
|
+
int x = (*fp2)(2,3);
|
128
96
|
|
129
|
-
|
97
|
+
//printf("%d",x);
|
130
|
-
|
131
|
-
sa.*p = 20;
|
132
|
-
|
133
|
-
sa.printf();
|
134
98
|
|
135
99
|
|
136
100
|
|
137
|
-
i
|
101
|
+
void (sample::*cfp)() = &sample::cf;
|
138
102
|
|
139
|
-
|
103
|
+
//(sa.*cfp)();
|
140
104
|
|
105
|
+
//(sa2.*cfp)();
|
106
|
+
|
141
|
-
p
|
107
|
+
//(sp->*cfp)();
|
142
108
|
|
143
109
|
|
144
110
|
|
145
|
-
|
111
|
+
int sample::*cp;
|
146
112
|
|
147
|
-
(sa.*pt)();
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
void(*fx)() = fp();
|
152
|
-
|
153
|
-
(*fx)();
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
int (*ff)(int a,int b) = fp2();
|
158
|
-
|
159
|
-
int x = (*ff)(1,2);
|
160
|
-
|
161
|
-
p
|
113
|
+
cp = &sample::cx;
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
114
|
|
167
115
|
|
168
116
|
|
@@ -172,6 +120,10 @@
|
|
172
120
|
|
173
121
|
|
174
122
|
|
123
|
+
|
124
|
+
|
125
|
+
_getch();
|
126
|
+
|
175
127
|
return 0;
|
176
128
|
|
177
129
|
}
|
2
質問したいことが明確でないのでわかりやすいように文章を編集しました。
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
c++
|
1
|
+
c++ クラスの関数ポインターの意味が知りたい箇所
|
test
CHANGED
@@ -1,26 +1,12 @@
|
|
1
|
-
質問タイトルの通り関数の戻り値に関数ポインターを設定したいのですがどうすればいいのでしょか?
|
2
|
-
|
3
|
-
typdef したりもしたのですが設定してもそのアドレスを何か変数に入れようとするとエラーになりそのエラーコードの編集方法がわかりません。
|
4
|
-
|
5
|
-
教えてくれますでしょうか?
|
6
|
-
|
7
|
-
エラーコード↓
|
8
|
-
|
9
|
-
[重大度レベル コード 説明 プロジェクト ファイル 行 抑制状態
|
10
|
-
|
11
|
-
|
1
|
+
クラスのメソッドや変数を関数ポインターとするときにint main関数部のコメント部 //1 や //2 ではインスタンスした変数を指定してますがそれをした以降そのインスタンス変数のポインターになるのでしょうか?それともsa2.*p = 20; とすることもできたため理解に困っています
|
12
|
-
|
13
|
-
]
|
14
2
|
|
15
3
|
|
16
4
|
|
17
|
-
|
18
|
-
|
19
|
-
```
|
5
|
+
```C++
|
20
6
|
|
21
7
|
#include <iostream>
|
22
8
|
|
23
|
-
#include "conio.h"
|
9
|
+
//#include "conio.h"
|
24
10
|
|
25
11
|
#include <math.h>
|
26
12
|
|
@@ -28,43 +14,45 @@
|
|
28
14
|
|
29
15
|
|
30
16
|
|
31
|
-
|
17
|
+
int add(int a, int b)
|
32
18
|
|
19
|
+
{
|
20
|
+
|
21
|
+
return a + b;
|
22
|
+
|
23
|
+
}
|
24
|
+
|
25
|
+
|
26
|
+
|
33
|
-
|
27
|
+
void f()
|
28
|
+
|
29
|
+
{
|
30
|
+
|
31
|
+
printf("関数ポインター");
|
32
|
+
|
33
|
+
}
|
34
|
+
|
35
|
+
|
36
|
+
|
37
|
+
//引数なし戻り値になし関数ポインタを返す関数
|
38
|
+
|
39
|
+
void (*fp())()
|
40
|
+
|
41
|
+
{
|
34
42
|
|
35
43
|
|
36
44
|
|
37
|
-
|
45
|
+
return f;
|
38
46
|
|
39
|
-
|
47
|
+
}
|
40
48
|
|
41
|
-
|
49
|
+
//戻り値ありで仮引数がある関数ポインタを返す関数
|
42
50
|
|
43
|
-
}
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
void f();
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
51
|
+
int (*fp2())(int a,int b)
|
52
|
-
|
53
|
-
};
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
void test::f()
|
58
52
|
|
59
53
|
{
|
60
54
|
|
61
|
-
int (test::*fp)(int a,int b) = &test::add;
|
62
|
-
|
63
|
-
int x = (this->*fp)(1,2);
|
64
|
-
|
65
|
-
|
55
|
+
return add;
|
66
|
-
|
67
|
-
|
68
56
|
|
69
57
|
}
|
70
58
|
|
@@ -72,93 +60,107 @@
|
|
72
60
|
|
73
61
|
|
74
62
|
|
63
|
+
|
64
|
+
|
75
|
-
|
65
|
+
class sample
|
76
66
|
|
77
67
|
{
|
78
68
|
|
79
|
-
|
69
|
+
public:
|
80
70
|
|
81
|
-
|
71
|
+
int cx = 0;
|
72
|
+
|
73
|
+
int cy = 0;
|
82
74
|
|
83
75
|
|
84
76
|
|
85
|
-
void
|
77
|
+
void f2()
|
86
78
|
|
87
|
-
{
|
79
|
+
{
|
88
80
|
|
89
|
-
int
|
81
|
+
printf("クラス関数");
|
90
82
|
|
91
|
-
|
83
|
+
}
|
92
84
|
|
93
|
-
|
85
|
+
int fs(int x)const
|
94
86
|
|
87
|
+
{
|
88
|
+
|
95
|
-
|
89
|
+
printf("samlpeクラス");
|
90
|
+
|
91
|
+
return x;
|
92
|
+
|
93
|
+
}
|
96
94
|
|
97
95
|
|
98
96
|
|
99
|
-
print
|
97
|
+
void print()
|
100
98
|
|
101
|
-
|
99
|
+
{
|
102
100
|
|
103
|
-
|
101
|
+
cout << cx << endl;
|
104
102
|
|
103
|
+
cout << cy << endl;
|
104
|
+
|
105
|
-
}
|
105
|
+
}
|
106
|
+
|
107
|
+
};
|
106
108
|
|
107
109
|
|
108
|
-
|
109
|
-
void (* fr())
|
110
|
-
|
111
|
-
{
|
112
|
-
|
113
|
-
return f;
|
114
|
-
|
115
|
-
}
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
int adds(int a, int b)
|
122
|
-
|
123
|
-
{
|
124
|
-
|
125
|
-
return a + b;
|
126
|
-
|
127
|
-
}
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
typedef int(*xx)();
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
xx t()
|
136
|
-
|
137
|
-
{
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
}
|
142
110
|
|
143
111
|
int main()
|
144
112
|
|
145
113
|
{
|
146
114
|
|
115
|
+
sample sa;
|
116
|
+
|
147
|
-
|
117
|
+
sample sa2;
|
148
118
|
|
149
119
|
|
150
120
|
|
151
|
-
|
121
|
+
int sample::*p;
|
152
122
|
|
153
|
-
|
123
|
+
p = &sample::cx;
|
124
|
+
|
125
|
+
sa2.*p = 10;//2
|
126
|
+
|
127
|
+
sa.print();
|
128
|
+
|
129
|
+
sa2.print();
|
130
|
+
|
131
|
+
sa.*p = 20;
|
132
|
+
|
133
|
+
sa.printf();
|
154
134
|
|
155
135
|
|
156
136
|
|
137
|
+
int(sample::*ptr)(int a )const = &sample::fs;
|
138
|
+
|
139
|
+
int aa = (sa.*ptr)(5);//1
|
140
|
+
|
141
|
+
printf("%d",aa);
|
157
142
|
|
158
143
|
|
159
144
|
|
145
|
+
void (sample::*pt)() = &sample::f2;
|
146
|
+
|
147
|
+
(sa.*pt)();
|
160
148
|
|
161
149
|
|
150
|
+
|
151
|
+
void(*fx)() = fp();
|
152
|
+
|
153
|
+
(*fx)();
|
154
|
+
|
155
|
+
|
156
|
+
|
157
|
+
int (*ff)(int a,int b) = fp2();
|
158
|
+
|
159
|
+
int x = (*ff)(1,2);
|
160
|
+
|
161
|
+
printf("%d",x);
|
162
|
+
|
163
|
+
|
162
164
|
|
163
165
|
|
164
166
|
|
@@ -176,8 +178,4 @@
|
|
176
178
|
|
177
179
|
|
178
180
|
|
179
|
-
|
180
|
-
|
181
181
|
```
|
182
|
-
|
183
|
-
```
|
1
linuxでもコンパイルできるようにWindows固有のものを削除。
test
CHANGED
File without changes
|
test
CHANGED
@@ -90,9 +90,9 @@
|
|
90
90
|
|
91
91
|
int b;
|
92
92
|
|
93
|
-
scanf
|
93
|
+
scanf("%d",&a);
|
94
94
|
|
95
|
-
scanf
|
95
|
+
scanf("%d",&b);
|
96
96
|
|
97
97
|
|
98
98
|
|
@@ -168,7 +168,7 @@
|
|
168
168
|
|
169
169
|
|
170
170
|
|
171
|
-
|
171
|
+
|
172
172
|
|
173
173
|
return 0;
|
174
174
|
|