函数定义和函数类型 [英] Function definition and function type
问题描述
我有以下代码,可以正常工作:
#include< iostream>
using namespace std;
typedef int(TMyFunc)(int);
TMyFunc * p;
int x(int y)
{
return y * 2;
}
int main()
{
p =& x;
cout<< (* p)(5)<< endl;
}
我想要做的是跳过定义 x
并直接定义 p
。像
TMyFunc p; p(y){return y * 2;}
。
有可能吗?如果是这样怎么办呢?如果不是为什么?
编辑:
看到答案后,我想我应该澄清:定义是分开的。即函数定义将在共享对象中。应用程序将通过 dlsym
获取函数的函数指针。我不想要一个函数对象。我想要的是知道如果我可以定义一个函数使用它的共享对象和应用程序共同的头文件的类型将提供。
这里有一个头文件它包含在应用程序和共享对象中:
#define FNAME_GET_FACTORYGetFactory
#define FNAME_GET_FUNCTION_IDS GetFunctionIDs
#define FNAME_GET_PLUGIN_INFOGetPluginInfo
typedef FunctionFactory *(* TpfGetFactory)();
typedef size_t(* TpfGetFunctionIDs)(int **);
typedef PluginInfo *(* TpfGetPluginInfo)();
在应用程序中,会发生类似的情况:
TpfGetFactory pF =(TpfGetFactory)dlsym(pHandle,FNAME_GET_FACTORY);
//使用pF来处理任何事情
现在, code> GetFactory 如下所示:
externCFunctionFactory * FNAME_GET_FACTORY(){// CODE}
忘记 externC
现在,我可以使用已定义的类型 TpfGetFactory
定义此函数? (这不是一个巨大的问题,我知道 - 但我很好奇,是否可能:))。我想要的是这样的共享对象:
TpfGetFactory f;
f(){// Implementation}
EDIT3:
我的尝试:
#include< iostream>
using namespace std;
typedef int(TF)(int);
TF f;
f(int x)
{
return x * 2;
}
int main()
{
x(3);
}
main.cpp:9:错误:ISO C ++禁止声明~f,没有类型
main.cpp:在函数中()’:
main.cpp:16:错误:â€~xâ€未在此范围内声明
这可能在C ++ 1x,下一个C ++标准,通常预计明年(这将使C ++ 11)。它允许这个:
auto p = [](int y){return y * 2;};
这取决于 auto
意思(从初始化它的表达式中自动推导出这个变量的类型)和新的lambda函数(允许在运行中创建函数)。
您的编译器实际上已经支持这个功能。
I have the following code which works as expected:
#include <iostream>
using namespace std;
typedef int (TMyFunc)(int);
TMyFunc* p;
int x(int y)
{
return y*2;
}
int main()
{
p = &x;
cout << (*p)(5) << endl;
}
What I want to do is skip defining x
and define p
there straight. Something like
TMyFunc p; p(y){return y*2;}
.
Is that possible? If so how do I do it? If not why?
EDIT:
After seeing the answers, I think I should clarify: I want the definition to be separate. i.e. function definition will be in a shared object. An application will acquire a function pointer to the function via dlsym
. I do not want a function object. What I want is to know if I can define a function using its type which a header file common to both the shared object and the application will provide. I hope that came out right :).
EDIT2: For sbi :)
This resides in a header which is included in both the application and the shared object:
#define FNAME_GET_FACTORY "GetFactory"
#define FNAME_GET_FUNCTION_IDS "GetFunctionIDs"
#define FNAME_GET_PLUGIN_INFO "GetPluginInfo"
typedef FunctionFactory* (*TpfGetFactory)();
typedef size_t (*TpfGetFunctionIDs)(int**);
typedef PluginInfo* (*TpfGetPluginInfo)();
In the application, something like this happens:
TpfGetFactory pF = (TpfGetFactory)dlsym(pHandle, FNAME_GET_FACTORY);
//Use pF for anything
Now, to do this, I have to define GetFactory
as follows in the shared object:
extern "C" FunctionFactory* FNAME_GET_FACTORY(){//CODE}
Forgetting the extern "C"
part for now, Can I define this function using the type TpfGetFactory
which is already defined? (This is not a huge issue I know - but I am curious as to whether it is possible :) ). What I want is something like this in the shared object :
TpfGetFactory f;
f(){//Implementation}
EDIT3:
My try:
#include <iostream>
using namespace std;
typedef int (TF)(int);
TF f;
f(int x)
{
return x*2;
}
int main()
{
x(3);
}
main.cpp:9: error: ISO C++ forbids declaration of ‘f’ with no type
main.cpp: In function ‘int main()’:
main.cpp:16: error: ‘x’ was not declared in this scope
It's possible in C++1x, the next C++ standard, generally expected next year (which would make it C++11, then). It allows this:
auto p = [](int y){return y*2;};
This relies on auto
been given a new meaning ("automatically deduce the type of this variable from the expression that initializes it") and the new lambda functions (allowing to create functions on the fly).
Your compiler might actually already support this.
这篇关于函数定义和函数类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!