J*****n 发帖数: 4859 | 1 #include "stdafx.h"
#include
#include
using namespace std;
typedef int (*T)(int, int);
int f(int a, int b=5){return a+b;}
int _tmain(int argc, _TCHAR* argv[])
{
T d=f;
cout<
system("pause");
}
谢谢 |
a*******h 发帖数: 123 | 2 感觉是没有办法,像你这个T就是一个固定的取两个 int 参数的类型,不能再变了。C+
+这种强类型的语言,不觉得除了用模板以外可以在类型上有任何灵活性。
【在 J*****n 的大作中提到】 : #include "stdafx.h" : #include : #include : using namespace std; : typedef int (*T)(int, int); : int f(int a, int b=5){return a+b;} : int _tmain(int argc, _TCHAR* argv[]) : { : T d=f; : cout<
|
w*****e 发帖数: 197 | 3 还是老老实实用个functor吧
【在 J*****n 的大作中提到】 : #include "stdafx.h" : #include : #include : using namespace std; : typedef int (*T)(int, int); : int f(int a, int b=5){return a+b;} : int _tmain(int argc, _TCHAR* argv[]) : { : T d=f; : cout<
|
B*****t 发帖数: 335 | 4 you need to deference the pointer,
cout<<(*d)(2, 2)<
【在 J*****n 的大作中提到】 : #include "stdafx.h" : #include : #include : using namespace std; : typedef int (*T)(int, int); : int f(int a, int b=5){return a+b;} : int _tmain(int argc, _TCHAR* argv[]) : { : T d=f; : cout<
|
J*****n 发帖数: 4859 | 5
对functor理解不深刻,请教一下大概该怎么写呢?
谢谢。
【在 w*****e 的大作中提到】 : 还是老老实实用个functor吧
|
S*********g 发帖数: 5298 | 6 define a class and use operator ()
http://en.wikipedia.org/wiki/Function_object
【在 J*****n 的大作中提到】 : : 对functor理解不深刻,请教一下大概该怎么写呢? : 谢谢。
|
J*****n 发帖数: 4859 | 7
这个我知道啊,不过问题是,我这里要调用有default para的函数,用functor怎么实
现呢?
谢谢。
【在 S*********g 的大作中提到】 : define a class and use operator () : http://en.wikipedia.org/wiki/Function_object
|
w*****e 发帖数: 197 | 8 struct functor {
T *m_f;
functor( T *f ) : m_f( f ) { ; }
void operator() ( int arg1 ) const {
( *m_f )( arg1 ); // this will pass compiler
}
};
That's it.
functor d( f );
d( 1 );
【在 J*****n 的大作中提到】 : : 这个我知道啊,不过问题是,我这里要调用有default para的函数,用functor怎么实 : 现呢? : 谢谢。
|
w*****e 发帖数: 197 | 9 Shoot, this will NOT work.
Sorry for the confusion.
I think you are stuck in some sense.
【在 w*****e 的大作中提到】 : struct functor { : T *m_f; : functor( T *f ) : m_f( f ) { ; } : void operator() ( int arg1 ) const { : ( *m_f )( arg1 ); // this will pass compiler : } : }; : That's it. : functor d( f ); : d( 1 );
|
S*********g 发帖数: 5298 | 10 #include
using namespace std;
typedef int (*T)(int, int);
class TT
{
public:
TT::TT(T f):ptr_f(f){}
int TT::operator()(int a, int b=5)
{
return ptr_f(a,b);
}
private:
T ptr_f;
};
int f(int a, int b=5){return a+b;}
int main(int argc, char* argv[])
{
TT d(f);
cout<
system("pause");
}
【在 J*****n 的大作中提到】 : : 这个我知道啊,不过问题是,我这里要调用有default para的函数,用functor怎么实 : 现呢? : 谢谢。
|
j*****4 发帖数: 292 | 11 #include
using namespace std;
class add {
public:
int operator()(int A, int B=5) {
return A+B;
}
};
int main() {
add functor;
cout<
system("pause");
}
【在 w*****e 的大作中提到】 : Shoot, this will NOT work. : Sorry for the confusion. : I think you are stuck in some sense.
|
S*********g 发帖数: 5298 | 12 The operator() can have default argument itself.
Below is an simple example that just works for your specific case.
If you want to have a more general solution, you will have to tell the
functor the information about the default argument in your function f in its
constructor.
You can achieve this by setting up a trait to pass along with you function
pointer.
#include
#include
using namespace std;
typedef int (*T)(int, int);
class TT
{
public:
TT::TT(T f):ptr_f(f){}
【在 J*****n 的大作中提到】 : : 这个我知道啊,不过问题是,我这里要调用有default para的函数,用functor怎么实 : 现呢? : 谢谢。
|