C ++中的指针到成员 - > *和。*运算符是什么? [英] What are the Pointer-to-Member ->* and .* Operators in C++?

查看:130
本文介绍了C ++中的指针到成员 - > *和。*运算符是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是,我看到此问题 此常见问题(错误链接) 此常见问题,但我仍然不明白 - > * 。* 意味着在C ++中。

这些页面提供有关



- > * / code>和在C ++中,你需要什么时候使用它们 - >

我希望这个例子能够清楚的说明这个例子。 为你

  //我们有一个类
struct X
{
void f ){}
void g(){}
};

typedef void(X :: * pointer)();
// ok,让我们拿一个指针并赋值给它。
pointer somePointer =& X :: f;
//现在我想调用somePointer。但是,为此,我需要一个对象
X x;
//现在我调用x上的成员函数,像这样
(x。* somePointer)(); //将调用x.f()
//现在,假设x不是对象,而是对象的指针
X * px = new X;
//我想调用px上的memfun指针。我使用 - > *
(px - > * somePointer)(); //将调用px-> f();

现在,您不能使用 x.somePointer c $ c>或 px-> somePointer(),因为在类X中没有这样的成员。为此,使用特殊成员函数指针调用语法...只是自己尝试几个例子,你会习惯它


Yes, I've seen this question and this FAQ (wrong link) this FAQ, but I still don't understand what ->* and .* mean in C++.
Those pages provide information about the operators (such as overloading), but don't seem to explain well what they are.

What are ->* and .* in C++, and when do you need to use them as compared to -> and .?

解决方案

I hope this example will clear things for you

//we have a class
struct X
{
   void f() {}
   void g() {}
};

typedef void (X::*pointer)();
//ok, let's take a pointer and assign f to it.
pointer somePointer = &X::f;
//now I want to call somePointer. But for that, I need an object
X x;
//now I call the member function on x like this
(x.*somePointer)(); //will call x.f()
//now, suppose x is not an object but a pointer to object
X* px = new X;
//I want to call the memfun pointer on px. I use ->*
(px ->* somePointer)(); //will call px->f();

Now, you can't use x.somePointer(), or px->somePointer() because there is no such member in class X. For that the special member function pointer call syntax is used... just try a few examples yourself ,you'll get used to it

这篇关于C ++中的指针到成员 - > *和。*运算符是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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