->* 运算符到底是什么?
我以前从未使用过它,只是在一篇文章中偶然发现它......我认为它相当于 *x->y
但显然事实并非如此。
这是我尝试过的,但给了我一个错误:
struct cake {
int * yogurt;
} * pie;
int main(void) {
pie = new cake;
pie->yogurt = new int;
return pie->*yogurt = 4;
}
I've never used it before and just stumbled upon it in an article... I thought it would be the equivalent to *x->y
but apparently it isn't.
Here's what I tried, and gave me an error:
struct cake {
int * yogurt;
} * pie;
int main(void) {
pie = new cake;
pie->yogurt = new int;
return pie->*yogurt = 4;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
当您有指向成员函数的指针时使用它。
当你有一个指向类的函数的指针时,你调用它的方式与调用任何成员函数
object.membername( ... )
或
objectptr->membername( ... )
几乎相同,但是当你有一个成员函数指针,后面需要加一个*。或->以便编译器理解接下来是一个变量,而不是要调用的函数的实际名称。
这是一个如何使用它的示例。
Its used when you have pointers to member functions.
When you have a pointer to a function of a class, you call it in much the same way you would call any member function
object.membername( ... )
or
objectptr->membername( ... )
but when you have a member function pointer, an extra * is needed after the . or -> in order that the compiler understand that what comes next is a variable, not the actual name of the function to call.
Here's an example of how its used.
它定义了一个指向成员的指针。
It defines a pointer to a member.
成员指针运算符:.* 和 ->*
Pointer-to-Member Operators: .* and ->*
.* 和 ->* 运算符将指向类或结构的成员函数。如果更改行,下面的代码将显示如何使用 .* 运算符的简单示例:
Value funcPtr = &Foo::One;
到Value funcPtr = &Foo::Two;
显示的结果将更改为 1000,因为该函数是inValue *2
例如取自此处:
The .* and ->* operators will point to member functions of a class or structure. The code below will show a simple example of how to use the .* operator, if you change the line:
Value funcPtr = &Foo::One;
toValue funcPtr = &Foo::Two;
the result displayed will change to 1000 since that function isinValue*2
for example Taken From Here: