C ++ typedef成员函数签名语法 [英] C++ typedef member function signature syntax
问题描述
我想声明一个成员函数签名的类型定义。全局函数typedef如下:
typedef int(function_signature)(int,int)
typedef int(* function_pointer)(int,int);
但是我不能为成员函数做同样的事情:
typedef int(foo :: memberf_signature)(int,int); // memberf_pointer不是foo
的成员typedef int(foo :: * memberf_pointer)(int,int);
这听起来像是逻辑上的,因为foo ::ist语法访问类foo。
我如何只是签名typedef
对于有关尴尬函数指针语法的问题,我个人使用cheat-sheet:函数指针教程( 可在此下载,感谢向量以指出它)。
然而,成员函数的签名有点不同于
你可能知道,一个成员函数有一个隐藏的参数 this
typedef int(Foo :: * Member)(int,int)
可以指定传递给函数的第一个元素是 Foo *
(因此你的方法真的需要3个参数,当你想到它,而不只是2。
,用于强制您指定类型。
函数指针可能指的是一个虚函数,在这种情况下事情可能会变得相当复杂,因此,事实上,在Visual Studio中,函数指针的大小可能在常规指针大小的1和4倍之间变化,这取决于函数是否是函数的大小virtual,特别是。
因此,函数引用的类是签名的一部分,并且没有解决方法。 p>
I want to declare type definition for a member function signature. Global function typedefs look like this:
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?
For questions regarding the awkward function pointer syntax, I personally use a cheat-sheet: The Function Pointers Tutorial (downloadable here, thanks to Vector for pointing it out).
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.
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.
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 size of 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.
Therefore, the class the function refers to is part of the signature, and there is no work-around.
这篇关于C ++ typedef成员函数签名语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!