Использование метода класса как функции в качестве типизированного аргумента в методе класса
08.09.2018, 04:28. Показов 1159. Ответов 4
Использование метода класса как функции в качестве типизированного аргумента в методе класса C++:
как правильно использовать метод класса (double f_tointegr (...) , ) как функцию ( вводимую по шаблону ) в качестве типизированного аргумента ( func_tointegr в DuhamelParser:  uhamelInt) и передавать ему аргументы в методе класса?
Как объявлять и использовать указатели на такие функции как аргументы, как объявлять их в методах , нужен ли const ?
| C++ | 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
| // trapintparam.cpp : Defines the entry point for the console application.
//
#include "stdafx.h" //
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <math.h>
typedef double (*func) ( double x ) ;
typedef struct
{
func Uinp ;
double tbegin ;
double tend ;
} Signalarray ;
typedef double (*funcparamdint )( double tau,double t , double eps , const func Uin , const func hfun ) ;
/* **************************************** */
class DuhamelParser {
public:
double Derivative ( double t , double eps , const func uinp );
double trapparam ( double a,double b,double t,double eps,double eps2, const funcparamdint intfunc, const func uinp ,
const func hfun );
double DuhamelInt( Signalarray *Signal ,int num, double t, double eps , double eps2 , const func hfun );
double GetSignal(double t , Signalarray *Signal1,int num );
private:
double getht( const func hfun, double t );
double f_tointegr( double tau , double t, double eps , const func Uin , const func hfun ) ;
double deriveU ( double tau , double eps1 , const func Uin );
};
double DuhamelParser::Derivative ( double t , double eps , const func uinp )
{
return ( uinp(t+eps) - uinp(t ) )/eps;
}
double DuhamelParser::deriveU ( double tau , double eps1 , const func Uin )
{
return Derivative (tau, 1e-8, Uin );
}
double DuhamelParser::f_tointegr( double tau , double t, double eps , const func Uin , const func hfun )
{
double eps1=1e-8;
return deriveU(tau ,eps, Uin )*hfun(t-tau);
}
double DuhamelParser::trapparam (double a,double b,double t,double eps,double eps2,const funcparamdint intfunc, const func uinp , const func hfun )
{
double h1,s,s0,s1,sn ,fun1,fun2,fun3 ;
int i,n ;
s=1;
sn=101;
n=4;
fun1=intfunc(a,t ,eps2 , uinp , hfun );
fun2=intfunc(b,t ,eps2 , uinp , hfun );
s0=(fun1+fun2)/2;
s1=intfunc ( ((a+b)/2),t ,eps2 , uinp , hfun );
while (fabs(s-sn)>eps)
{
sn =s;
h1 =(b-a)/n;
i=0 ;
while (i<(int)(n/2) )
{
fun3 = intfunc( ( a+(2*i+1)*h1 ) , t ,eps2 , uinp , hfun ) ;
s1 =s1+fun3;
i =i+1;
}
s =h1*(s0+s1);
n =n*2;
}
return s;
}
double DuhamelParser::getht( const func hfun, double t )
{
return hfun(t);
}
double DuhamelParser::DuhamelInt( Signalarray *Signal ,int num, double t, double eps , double eps2 , const func hfun )
{
double dUinp_Ht ;
double y ;
double te , tp ;
int n,m ,i ;
const funcparamdint func_tointegr;
func_tointegr= DuhamelParser::f_tointegr ;
//Signalarray *Signal =new Signalarray[num];
m =0;
for(i=0;i<num;i++)
{
if(i==0)
{ if((t>=0 ) &&(t<= Signal[0].tend)) { m =0; } }
else
{ if((t>= Signal[i].tbegin)&&(t<= Signal[i].tend)) { m =i; } }
}
if ((t> Signal[num-1].tend) ) { std::cout<<"end of signal\n" ; exit(1); }
y =0;
for (i =0; i<= m ; i++)
{
if (i==0) { y=y+Signal[0].Uinp(0)*getht(hfun,t) +trapparam(0,t,t,eps,eps2, func_tointegr, Signal[0].Uinp, hfun ); }
else
{
tp =Signal[i].tbegin ;
te =Signal[i].tend ;
if (i==m) te =t; ;
dUinp_Ht=( Signal[i].Uinp(tp)-Signal[i-1].Uinp (tp) )*getht(hfun,t-tp);
y=y+dUinp_Ht+trapparam(tp,te, t,eps,eps2, func_tointegr, Signal[i].Uinp , hfun );
}
}
delete[] Signal ;
return y;
}
double DuhamelParser::GetSignal(double t , Signalarray *Signal1, int num )
{
int i, m ;
//Signalarray *Signal1= new Signalarray[4];
m =0;
for(i=0;i<num;i++)
{
if(i==0)
{ if((t>=0 ) &&(t<= Signal1[0].tend)) { m =0; } }
else
{ if((t>= Signal1[i].tbegin)&&(t<= Signal1[i].tend)) { m=i; } }
}
if ((t> Signal1[num-1].tend) ) { std::cout<<"end of signal\n" ; return 0 ; }
return Signal1[m].Uinp(t);
}
/****************************************************/
Signalarray AssignSignal( const func Ui , double tbeg , double tend )
{
Signalarray Uinput ;
Uinput.Uinp =Ui;
Uinput.tbegin =tbeg;
Uinput.tend =tend;
return Uinput;
}
/*********************************************/
double h1 ( double t )
{
double R,C,k,Tau ;
R =1000;
C =0.15e-6;
k =1; //0.25
Tau =R*C;
if (t<0) { return 0; } else { return k*( exp(-t/Tau)) ; }
}
double h2( double t )
{
double R,C,k,Tau ;
R =1000;
C =0.15e-6;
k =1; //0.25
Tau =R*C;
if (t<0) { return 0 ; } else { return k*(1-exp(-t/Tau)) ; }
}
double Uinp1( double t )
{
return 1 ; //
}
double Uinp2(double t )
{
return 0; //
}
double Uinp3( double t )
{
return 1; //
}
double Uinp4(double t )
{
return 0; //
}
Signalarray *InputSignals( )
{
int num;
num=4;
Signalarray *Signal2 =new Signalarray[num];
Signal2[0] =AssignSignal( Uinp1 ,0, 1e-3 ) ; //[0,t1]
Signal2[1] =AssignSignal( Uinp2 ,1e-3,2e-3 ) ; //[t1,t2]
Signal2[2] =AssignSignal( Uinp3 ,2e-3,3e-3 ) ; //[t2,t3]
Signal2[3] =AssignSignal( Uinp4 ,3e-3,4e-3 ) ; //[t3,t4]
return Signal2;
}
/*
u
t=[0 ,t1]
Y1(t) =U1(0)*h(t)+ integr(0;t;derivU(tau)*h(t-tau); dtau) //0..t1
Отклик на остальных интервалах вычисляется по формулам, вытекающих из принципа суперпозиции:
Y2(t)=U1(0)*h(t)+integr(0;t1;derivU1(tau)*h(t-tau); dtau)+
+(U2(t1)-U1(t1))*(h(t-t1)) +integr(t1; t ; derivU2(tau)*h(t-tau); dtau)+
Y3= U1(0)*h(t) + integr(0;t1;derivU1(tau)*h(t-tau); dtau) +
+(U2(t1)-U1(t1))*(h(t-t1)) +integr(t1; t2 ; derivU2(tau)*h(t-tau); dtau)+
+(U3(t2)-U2(t2))*(h(t-t2)) +integr(t2; t ; derivU3(tau)*h(t-tau); dtau)
*/
//for dummy example , if U(t):=U(0)*h(t)+ integr(0;t;derivU(tau)*h(t-tau); dtau)
//U(t):=U(0)*h(t)+ integr(0;t;derivU(tau)*h(t-tau); dtau)
//U(t):=U(0)*h(t)+ integr(0;t;derivU(tau)*h(t-tau); dtau)
int main (void)
{
FILE *fp;
double time,Tend,step, y,x ;
Signalarray *Signals1;
DuhamelParser Parser1;
Tend =3;
std::cout<<" Input Tend, ms (4) " ;
std::cin>> Tend;
std::cout<< Tend<<"\n";
Tend =Tend*0.001 ;
std::cout<<" Input step ,ms,(0.1) " ;
step =0.1;
std::cin>> step ;
std::cout<<step<<"\n";
step =step*0.001;
if ((fp=fopen("Integral.txt","w"))==NULL) { printf ("Error."); exit(1); }
Signals1= InputSignals( );
fprintf(fp," Tbeg=0 , Tend=%lf \n", Tend);
fprintf (fp," step= %le s \n" ,step );
fprintf(fp," h1 =k*( exp(-t/Tau) \n" );
time =0 ;
while (time<=Tend) {
y=Parser1.DuhamelInt(Signals1,4,time,1e-6,1e-8, h2);
x=Parser1.GetSignal(time, Signals1,4);
printf (" t= %le s y(t)= %le V; x=%le V \n" ,time,y,x);
fprintf(fp," t= %le y(t)=%le V; x=%le V \n", time, y,x);
time =time+step ;
}
delete[] Signals1;
char key;
std::cout<<"\nInput 0 to exit ";
std::cin>>key;
return 0;
} |
|
| C++ | 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
| // stdafx.h : include file for standard system include files,
// or project specific include files that are used frequently, but
// are changed infrequently
//
#pragma once
#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
#include <stdio.h>
//#include <tchar.h>
// TODO: reference additional headers your program requires here |
|
В Visual Studio Express 2005 (VC++,console application )
| Code | 1
2
3
4
5
6
7
8
9
| 1>------ Build started: Project: trapintparam, Configuration: Debug Win32 ------
1>Compiling...
1>trapintparam.cpp
1>c:\users\user_pc01\desktop\folder2\trapintparam2\trapintparam\trapintparam.cpp(139) : error C2734: 'func_tointegr' : const object must be initialized if not extern
1>c:\users\user_pc01\desktop\folder2\trapintparam2\trapintparam\trapintparam.cpp(140) : error C2440: '=' : cannot convert from 'double (__thiscall DuhamelParser::* )(double,double,double,const func,const func)' to 'const funcparamdint'
1> There is no context in which this conversion is possible
1>Build log was saved at "file://c:\Users\USER_PC01\Desktop\folder2\trapintparam2\trapintparam\Debug\BuildLog.htm"
1>trapintparam - 2 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ========== |
|
Добавлено через 3 минуты
Версия без класса, которая работает :
| C++ | 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
| // trapintparam.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <math.h>
typedef double (*func) ( double x ) ;
typedef struct
{
func Uinp ;
double tbegin ;
double tend ;
} Signalarray ;
typedef double (*funcparamdint )( double tau,double t , double eps , const func Uin , const func hfun ) ;
void writeln ( char *str )
{
std::cout<<"\n"<<str<<"\n";
return;
}
double Derivative ( double t , double eps , const func uinp )
{
return ( uinp(t+eps) - uinp(t ) )/eps;
}
double deriveU ( double tau , double eps1 , const func Uin )
{
return Derivative (tau, 1e-8, Uin );
}
double f_tointegr( double tau , double t, double eps , const func Uin , const func hfun )
{
double eps1=1e-8;
return deriveU(tau ,eps, Uin )*hfun(t-tau);
}
double trapparam ( double a, double b, double t, double eps, double eps2, const funcparamdint intfunc, const func uinp ,
const func hfun )
{
double h1,s,s0,s1,sn ,fun1,fun2,fun3 ;
int i,n ;
s=1;
sn=101;
n=4;
fun1=intfunc(a,t ,eps2 , uinp , hfun );
fun2=intfunc(b,t ,eps2 , uinp , hfun );
s0=(fun1+fun2)/2;
s1=intfunc ( ((a+b)/2),t ,eps2 , uinp , hfun );
while (fabs(s-sn)>eps)
{
sn =s;
h1 =(b-a)/n;
i=0 ;
while (i<(int)(n/2) )
{
fun3 = intfunc( ( a+(2*i+1)*h1 ) , t ,eps2 , uinp , hfun ) ;
s1 =s1+fun3;
i =i+1;
}
s =h1*(s0+s1);
n =n*2;
}
return s;
}
/****************************************************/
double h1 ( double t )
{
double R,C,k,Tau ;
R =1000;
C =0.15e-6;
k =1; //0.25
Tau =R*C;
if (t<0) { return 0; } else { return k*( exp(-t/Tau)) ; }
}
double h2( double t )
{
double R,C,k,Tau ;
R =1000;
C =0.15e-6;
k =1; //0.25
Tau =R*C;
if (t<0) { return 0 ; } else { return k*(1-exp(-t/Tau)) ; }
}
double Uinp1( double t )
{
return 1 ; //
}
double Uinp2(double t )
{
return 0; //
}
double Uinp3( double t )
{
return 1; //
}
double Uinp4(double t )
{
return 0; //
}
Signalarray AssignSignal( const func Ui , double tbeg , double tend )
{
Signalarray Uinput ;
Uinput.Uinp =Ui;
Uinput.tbegin =tbeg;
Uinput.tend =tend;
return Uinput;
}
double getht( const func hfun, double t )
{
return hfun(t);
}
double GetSignal(double t )
{
int m ;
Signalarray *Signal1= new Signalarray[4];
m =0;
Signal1[0] =AssignSignal( Uinp1 ,0, 1e-3 ) ; //[0,t1]
Signal1[1] =AssignSignal( Uinp2 ,1e-3,2e-3 ) ; //[t1,t2]
Signal1[2] =AssignSignal( Uinp3 ,2e-3,3e-3 ) ; //[t2,t3]
Signal1[3] =AssignSignal( Uinp4 ,3e-3,4e-3 ) ; //[t3,t4]
if((t>=0 )&&(t<= Signal1[0].tend) ) { m =0; }
if((t>= Signal1[1].tbegin)&&(t<= Signal1[1].tend)) { m =1; }
if((t>= Signal1[2].tbegin)&& (t<= Signal1[2].tend)) { m =2; }
if((t>= Signal1[3].tbegin)&& (t<= Signal1[3].tend)) { m =3; }
return Signal1[m].Uinp(t);
}
/*
u
t=[0 ,t1]
Y1(t) =U1(0)*h(t)+ integr(0;t;derivU(tau)*h(t-tau); dtau) //0..t1
Отклик на остальных интервалах вычисляется по формулам, вытекающих из принципа суперпозиции:
Y2(t)=U1(0)*h(t)+integr(0;t1;derivU1(tau)*h(t-tau); dtau)+
+(U2(t1)-U1(t1))*(h(t-t1)) +integr(t1; t ; derivU2(tau)*h(t-tau); dtau)+
Y3= U1(0)*h(t) + integr(0;t1;derivU1(tau)*h(t-tau); dtau) +
+(U2(t1)-U1(t1))*(h(t-t1)) +integr(t1; t2 ; derivU2(tau)*h(t-tau); dtau)+
+(U3(t2)-U2(t2))*(h(t-t2)) +integr(t2; t ; derivU3(tau)*h(t-tau); dtau)
*/
//for dummy example , if U(t):=U(0)*h(t)+ integr(0;t;derivU(tau)*h(t-tau); dtau)
//U(t):=U(0)*h(t)+ integr(0;t;derivU(tau)*h(t-tau); dtau)
//U(t):=U(0)*h(t)+ integr(0;t;derivU(tau)*h(t-tau); dtau)
double DuhamelInt( double t, double eps , double eps2 , func hfun )
{
double dUinp_Ht ;
double y ;
double te , tp ;
int n,m ,i ;
n =4;
Signalarray *Signal =new Signalarray[n];
//SetLength(Signal,n); //0,1,2- >U1,U2,U3
//patch for optimize signals functions mapping
Signal[0] =AssignSignal( Uinp1 ,0, 1e-3 ) ; //[0,t1]
Signal[1] =AssignSignal( Uinp2 ,1e-3,2e-3 ) ; //[t1,t2]
Signal[2] =AssignSignal( Uinp3 ,2e-3,3e-3 ) ; //[t2,t3]
Signal[3] =AssignSignal( Uinp4 ,3e-3,4e-3 ) ; //[t3,t4]
m =0;
//patch for optimize signals functions mapping, fix bugs
if((t>=0 ) &&(t<= Signal[0].tend)) { m =0; }
if((t>= Signal[1].tbegin)&&(t<= Signal[1].tend)) { m =1; }
if((t>= Signal[2].tbegin) &&(t<= Signal[2].tend)) { m =2; }
if((t>= Signal[3].tbegin) && (t<= Signal[3].tend)) { m =3; }
if ((t> Signal[3].tend) ) { std::cout<<"end of signal\n" ; exit(1); }
y =0;
for (i =0; i<= m ; i++)
{
if (i==0)
{
y=y+Signal[0].Uinp(0)*getht(hfun,t) +trapparam(0,t,t,eps,eps2, f_tointegr, Signal[0].Uinp, hfun ); //U1
}
else
{
tp =Signal[i].tbegin ;
te =Signal[i].tend ;
if (i==m) te =t; ;
dUinp_Ht=( Signal[i].Uinp(tp)-Signal[i-1].Uinp (tp) )*getht(hfun,t-tp);
y=y+dUinp_Ht+trapparam(tp,te, t,eps,eps2, f_tointegr, Signal[i].Uinp , hfun );
}
}
delete[] Signal ;
return y;
}
int main (void)
{
FILE *fp;
double time,Tend,step, y,x ;
Tend =3;
std::cout<<" Input Tend, ms (4) " ;
std::cin>> Tend;
std::cout<< Tend<<"\n";
Tend =Tend*0.001 ;
std::cout<<" Input step ,ms,(0.1) " ;
step =0.1;
std::cin>> step ;
std::cout<<step<<"\n";
step =step*0.001;
if ((fp=fopen("Integral.txt","w"))==NULL) { printf ("Error."); exit(1); }
fprintf(fp," Tbeg=0 , Tend=%lf \n", Tend);
fprintf (fp," step= %le s \n" ,step );
fprintf(fp," h1 =k*( exp(-t/Tau) \n" );
time =0 ;
while (time<=Tend) {
y =DuhamelInt(time, 1e-6,1e-8, h1 );
x=GetSignal(time );
printf (" t= %le s y(t)= %le V; x=%le V \n" ,time,y,x);
fprintf(fp," t= %le y(t)=%le V; x=%le V \n", time, y,x);
time =time+step ;
}
char key;
std::cout<<"\nInput 0 to continue ";
std::cin>>key;
fprintf(fp," Tbeg=0 , Tend=%lf \n", Tend);
fprintf (fp," step= %le s \n" ,step );
fprintf(fp," h2 = k*(1-exp(-t/Tau)) \n" );
time =0 ;
while (time<=Tend) {
y =DuhamelInt(time, 1e-6,1e-8, h2 );
x=GetSignal(time );
printf (" t= %le s y(t)= %le V; x=%le V \n" ,time,y,x);
fprintf(fp," t= %le y(t)=%le V; x=%le V \n", time, y,x);
time =time+step ;
}
fclose( fp);
std::cout<<"\nInput 0 to continue ";
std::cin>>key;
return 0;
} |
|
Как сделать ее с классом? Можно ли ее сделать без вложенных классов как функторов?
Пример с функторами из интернета
| C++ | 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
| #include <stdio.h>
#include <iostream>
// Abstract base class
class BinaryFunction {
public:
BinaryFunction() {};
virtual double operator() (double left, double right) = 0;
};
// Add two doubles
class Add : public BinaryFunction {
public:
Add() {};
virtual double operator() (double left, double right) { return left+right; }
};
// Multiply two doubles
class Multiply : public BinaryFunction {
public:
Multiply() {};
virtual double operator() (double left, double right) { return left*right; }
};
double binary_op(double left, double right, BinaryFunction* bin_func) {
return (*bin_func)(left, right);
}
int main( ) {
double a = 5.0;
double b = 10.0;
BinaryFunction* pAdd = new Add();
BinaryFunction* pMultiply = new Multiply();
std::cout << "Add: " << binary_op(a, b, pAdd) << std::endl;
std::cout << "Multiply: " << binary_op(a, b, pMultiply) << std::endl;
delete pAdd;
delete pMultiply;
char ch;
std::cin>>ch;
return 0;
} |
|
Добавлено через 9 минут
Нужно ли использовать template <typename T> (
https://ru.wikipedia.org/wiki/... 0.B8.D0.B9 )?
Добавлено через 1 час 28 минут
https://stackoverflow.com/ques... s-expected
Добавлено через 6 минут
Пример с объявлением функций и указателями :
| C++ | 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
| #include <iostream>
// the function using the function pointers:
void somefunction(void (*fptr)(void*, int, int), void* context) {
fptr(context, 17, 42);
}
void non_member(void*, int i0, int i1) {
std::cout << "I don't need any context! i0=" << i0 << " i1=" << i1 << "\n";
}
struct foo {
void member(int i0, int i1) {
std::cout << "member function: this=" << this << " i0=" << i0 << " i1=" << i1 << "\n";
}
};
void forwarder(void* context, int i0, int i1) {
static_cast<foo*>(context)->member(i0, i1);
}
int main() {
somefunction(&non_member, 0);
foo object;
somefunction(&forwarder, &object);
char key1;
std::cin>>key1;
} |
|
Добавлено через 2 часа 21 минуту
| C++ | 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
| // trapintparam.cpp : Defines the entry point for the console application.
//
//#include "stdafx.h"
#include <iostream>
#include <math.h>
using namespace std;
/* **************************************** */
// template <class T>
typedef double (*func) (double );
class DataParser // : public T
{
public:
DataParser(){ std::cout << "DataParser constructor passed.\n"; };
~DataParser(){ std::cout << "DataParser destructor passed.\n";};
double Xinp2( double t );
double Func1 ( double t , double arg2 , func xinp ); // ? //T xinp
};
double DataParser::Func1( double t , double arg2 , func xinp ) // ? //T xinp
{
double res1;
res1=xinp(t+arg2 );
return res1;
}
// double (DataParser ::*Xinp2)() =&DataParser :: Xinp2;
double DataParser::Xinp2( double t )
{
return 1e-3*t ;
}
double Xinp1( double t )
{
return 1e-3*t ; //
}
int main (void)
{
FILE *fp;
double time,Tend,step, y,x ;
DataParser Parser1;
time=0;
Tend=0.1;
step=0.01;
y=0;
//double DataParser::* pUinp2 = &DataParser::Uinp2;
while (time<=Tend) {
x= Parser1.Xinp2(time);
y= Parser1.Func1 ( time ,2.5 , Xinp1 );
//y= Parser1.Func1 ( time , 2.5 , pUinp2 );
printf (" t= %le s y(t)= %le ; x=%le \n" ,time,y,x);
time =time+step ;
}
Parser1.~DataParser();
char key;
std::cout<<"\nInput 0 to exit ";
std::cin>>key;
return 0;
} |
|
Как передать метод класса DataParser:: Uinp2(x) в выражение y= Parser1.Func1 ( time , 2.5 , DataParser::Uinp2 );
создав указатель на него ? Как поменять & ,* в func xinp и его обработчике ? С y= Parser1.Func1 ( time ,2.5 , Xinp1 ); работает, а с методом из класса "поинтерная" неадекватность .
0
|