C++ typedef 成员函数签名语法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4832275/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C++ typedef member function signature syntax
提问by 0xbadf00d
I want to declare type definition for a member function signature. Global function typedefs look like this:
我想为成员函数签名声明类型定义。全局函数 typedef 如下所示:
typedef int (function_signature)(int, int);
typedef int (*function_pointer) (int, int);
But I'm not able to the same thing for a member function:
但我不能为成员函数做同样的事情:
typedef int (foo::memberf_signature)(int, int); // memberf_pointer is not a member of foo
typedef int (foo::*memberf_pointer)(int, int);
It sounds logically to me, because "foo::" ist the syntax to access a member in the class foo. How can I typedef just the signature?
对我来说这听起来合乎逻辑,因为“foo::”是访问 foo 类成员的语法。我怎样才能 typedef 只是签名?
采纳答案by Matthieu M.
For questions regarding the awkward function pointer syntax, I personally use a cheat-sheet: The Function Pointers Tutorial(downloadable here, thanks to Vectorfor pointing it out).
对于有关笨拙的函数指针语法的问题,我个人使用了一个备忘单:函数指针教程(可在此处下载,感谢Vector指出)。
The signature of a member function, however, is a bit different from the signature of a regular function, as you experienced.
但是,正如您所经历的,成员函数的签名与常规函数的签名略有不同。
As you probably know, a member function has a hidden parameter, this
, whose type need be specified.
您可能知道,成员函数有一个隐藏参数 ,this
需要指定其类型。
// C++11 and above.
using Member = int (Foo::*)(int, int);
// C++03 and below.
typedef int (Foo::*Member)(int, int);
does let you specify that the first element passed to the function will be a Foo*
(and thus your method really takes 3 arguments, when you think of it, not just 2.
确实让您指定传递给函数的第一个元素将是 a Foo*
(因此您的方法实际上需要 3 个参数,当您想到它时,而不仅仅是 2 个。
However there is another reason too, for forcing you to specify the type.
但是还有另一个原因,强制您指定类型。
A function pointer might refer to a virtual function, in which case things can get quite complicated. Therefore, the very sizeof the in-memory representation changes depending on the type of function. Indeed, on Visual Studio, a function pointer's size might vary between 1 and 4 times the size of a regular pointer. This depends on whether the function is virtual, notably.
函数指针可能指向虚函数,在这种情况下事情会变得非常复杂。因此,内存中表示的大小会根据函数的类型而变化。实际上,在 Visual Studio 上,函数指针的大小可能是常规指针大小的 1 到 4 倍。这取决于函数是否是虚拟的,尤其是。
Therefore, the class the function refers to is part of the signature, and there is no work-around.
因此,函数引用的类是签名的一部分,没有变通方法。
回答by Nikos Athanasiou
You can factor out the target class in modern C++ (post 11) by utilizing the 'typedefing' qualitiesof template aliases. What you need would look like like:
您可以通过利用模板别名的“typedefing”特性,在现代 C++(后 11)中分解出目标类。你需要的看起来像:
template<typename T>
using memberf_pointer = int (T::*)(int, int);
Yet at the point of declaration, a pointer to member function utilizing this syntax would need to specify the target class:
然而,在声明时,使用此语法的指向成员函数的指针需要指定目标类:
// D is a member function taking (int, int) and returning int
memberf_pointer<foo> mp = &foo::D;
回答by TonyK
It works for me:
这个对我有用:
#include <iostream>
class foo
{
public:
int g (int x, int y) { return x + y ; }
} ;
typedef int (foo::*memberf_pointer)(int, int);
int main()
{
foo f ;
memberf_pointer mp = &foo::g ;
std::cout << (f.*mp) (5, 8) << std::endl ;
}
回答by Mark B
The reason it doesn't work with your current syntax is that operator precedence dictates that you're referring to a function named foo::memberf_signature
, not any sort of type.
它不适用于您当前的语法的原因是运算符优先级表明您指的是名为 的函数foo::memberf_signature
,而不是任何类型的类型。
I don't know for sure if you can do this or not, but I couldn't come up with any combination of parenthese that induced the code to compile with g++ 4.2.
我不确定您是否可以这样做,但我无法想出任何括号组合来诱导代码使用 g++ 4.2 进行编译。
回答by googling
Well basically it can't work (at least I know no way using g++); Using borland c++ compiler there would be the __closure keyword.
嗯,基本上它不能工作(至少我知道无法使用 g++);使用 borland C++ 编译器会有 __closure 关键字。
The reason why it does not compile is, that sizeof the functionpointer (on a x86 machine) occupies always <<32bits>>; but if you want to point to a class (interface) signature, the sizeof has to be 64bit: 32 bit for the this pointer (as the class interface is in the memory only once) and 32 bit for the actual function
它不编译的原因是,函数指针的大小(在 x86 机器上)总是占用 <<32bits>>;但是如果你想指向一个类(接口)签名,sizeof 必须是 64 位:this 指针为 32 位(因为类接口只在内存中一次)和 32 位用于实际函数
But the __closure keyword is a bcb language 'hack' not standardized...
但是 __closure 关键字是一种未标准化的 bcb 语言“hack”...