成员函数指针 [英] Member function pointer

查看:108
本文介绍了成员函数指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果C ++ FAQ Lite中的以下内容为真:函数名称衰减为指向该函数的指针"(因为数组名称衰减为为其第一个元素的指针);为什么我们必须包括&"符号?

If the following from the C++ FAQ Lite is true: "a function name decays to a pointer to the function" (as an array name decays to a pointer to its first element); why do we have to include the ampersand?

typedef  int (Fred::*FredMemFn)(char x, float y);
FredMemFn p = &Fred::f;

而不仅仅是:

typedef  int (Fred::*FredMemFn)(char x, float y);
FredMemFn p = Fred::f;

在第二种情况下,Fred :: f是一个函数,并且可以衰减为该函数的指针.

In the second case Fred::f is a function and can decay to a pointer to that function.

我希望这个问题不是那么愚蠢.

I hope this question is not that stupid.

推荐答案

原始答案:

Original answer:

因为成员函数不是函数,并且成员函数指针不是函数指针.因此,衰减规则不适用.

because a member-function is not a function and a member-function-pointer is not a function-pointer. Therefore the rules of decay don't apply.

此外,C ++中有函数类型,但没有成员函数类型.因此,可以在需要指向函数的指针的地方使用函数,但是不能使用成员函数,因为没有这样的东西,只有指针到成员函数.您的示例中的f是一个函数.另一方面,Fred :: f是……好吧,什么都没有.

Also, there is function type in C++, but not member-function type. So you can use a function in places where a pointer-to-function is expected, but you can't use a member-function because there is no such thing, only pointer-to-member-function. f in your example is a function. On the other hand, Fred::f is... well, nothing.

此外,我会说函数的名称可能会衰减...".不,名称不能执行任何操作,可以将函数类型的左值隐式转换为指向函数的指针,就重载解析而言,这是身份转换

Also, I would argue that "the name of a function can decay...". No, the name cannot do anything, an lvalue of function type can be implicitly converted to a pointer-to-function, and this is an identity conversion as far as overload resolution is concerned

编辑以澄清我的答案:

Editing to clarify my answer:

C ++中的每个表达式都有一个类型和值.一种类型的值有时可以转换为另一种类型的值.对这些转换进行排名,以便使一次转换比另一项转换更好,主要是针对函数重载解析.

Every expression in C++ has a type and value. A value of one type can occasionally be converted to a value of another type. These conversions are ranked so that to make one conversion better than another one mainly for function overload resolution.

转换的一种类型称为左值到右值转换.当左值出现在需要右值的上下文中时,将进行此转换.通常,这种转换不执行任何操作,例如:

One of the types of the conversions is called lvalue-to-rvalue conversion. When an lvalue appears in a context where an rvalue is required this conversion takes place. Usually this kind of conversion does nothing, for example:

int i = 4, j = 5;
i = j;

第二行上的

j是一个左值,但是这里需要一个右值,因此j被转换为一个右值.但这不是可观察的转换,对吗?但是在某些情况下,可以观察到左值到右值的转换.即,可以将n T数组的 左值转换为T*类型的右值,其值是数组的第一个元素的地址 类型为带有签名S的函数"的左值,类型为指向带有签名S的函数的指针"的右值,其值为函数的地址

on the second line j is an lvalue, but an rvalue is needed here, so j is converted to an rvalue. But this is not an observable conversion, is it? But there are cases where the lvalue-to-rvalue conversion can be observed. That is, an lvalue of array of n T can be converted to an rvalue of type T* whose value is the address of the first element of the array and an lvalue of type "function with signature S" an rvalue of type "pointer to function with signature S" whose value is the function's address

这意味着当我们将函数分配给指向函数的指针时,函数左值将隐式转换为其地址.

That means that when we assign a function to a pointer-to-function the function lvalue is implicitly converted to its address.

void f() {}
void (*p) () = f; //f is converted to rvalue

f是一个表达式,并且具有类型. f的类型是void()

f is an expression and has a type. f's type is void()

在C ++中,没有像member-function 这样的类型. 有指向成员函数的指针,但没有成员函数本身.我说的当然是非静态函数.静态函数的工作方式与普通函数相同,也就是说,您不必编写&X::f,而可以编写X::f 为什么?因为X :: f具有类型函数,并且发生上述转换.但是,如果f是非静态的,则X :: f的类型是....哦,是的,它没有类型,因此不是表达式,因此没有值,因此该值不能转换为任何东西.

There is no such type in C++ as a member-function There are pointers-to-member-functions, but not member functions themselves. I am talking of course about nonstatic functions. Static functions work the same way as ordinary functions, that is, you don't have to write &X::f, instead you can write X::f Why? Because X::f has a type function and the abovementioned conversion takes place. If f is nonstatic, however, X::f is of type... what? Oh yeah, it doesn't have a type and therefore is not an expression and therefore doesn't have value and therefore that value cannot be converted to anything.

引用标准:5.3.1第3条 仅当显式&使用,并且其操作数是不包含在括号中的限定ID. [注意:也就是说,表达式&(qualified-id)的括号中包含有qualified-id,它不构成"pointer to member"类型的表达式. qualified-id也不一样,因为从非静态成员函数的qualified-id到类型指针到成员函数的指针"没有隐式转换,就像从函数类型的左值到指针到函数的指针"类型一样(4.3 ). & unqualified-id也不是指向成员的指针,即使在 不合格ID的班级. ]

Quote from the standard: 5.3.1 clause 3 A pointer to member is only formed when an explicit & is used and its operand is a qualified-id not enclosed in parentheses. [Note: that is, the expression &(qualified-id), where the qualified-id is enclosed in parentheses, does not form an expression of type "pointer to member." Neither does qualified-id, because there is no implicit conversion from a qualified-id for a nonstatic member function to the type "pointer to member function" as there is from an lvalue of function type to the type "pointer to function" (4.3). Nor is &unqualified-id a pointer to member, even within the scope of the unqualified-id’s class. ]

希望这更清楚...

这篇关于成员函数指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆