由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Quant版 - 如何用函数指针调用含default parameter的函数?
相关主题
[合集] 请问一道C++题test C# lib with developed by others in visual studio 2013 (转载)
C++ 问题biuld error of C# reference of namespace in two projects (转载)
C++ online Test 2题找矿工的一些感想和经验-第一次
我想找人一起写codeCracking the code (长文介绍D.E. Shaw)
最大求和子序列面试问倒了金融工程程序大收集
(更新)求助:C++里的fstream究竟该怎么用?GMARCH parameter estimation
问面试题[合集] Basic Garch model implementation question.
C++学到什么程度能放到简历上?SV kalman filter parameter estimation
相关话题的讨论汇总
话题: int话题: functor话题: tt话题: include话题: operator
进入Quant版参与讨论
1 (共1页)
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怎么实
: 现呢?
: 谢谢。

1 (共1页)
进入Quant版参与讨论
相关主题
SV kalman filter parameter estimation最大求和子序列面试问倒了
How to use SAS to send email via Lotus Notes?(更新)求助:C++里的fstream究竟该怎么用?
matlab fminunc for GARCH model问面试题
一道题C++学到什么程度能放到简历上?
[合集] 请问一道C++题test C# lib with developed by others in visual studio 2013 (转载)
C++ 问题biuld error of C# reference of namespace in two projects (转载)
C++ online Test 2题找矿工的一些感想和经验-第一次
我想找人一起写codeCracking the code (长文介绍D.E. Shaw)
相关话题的讨论汇总
话题: int话题: functor话题: tt话题: include话题: operator