是的,我看过this questionthis FAQ,但是我仍然不明白->*.*在C++中的含义。
这些页面提供了有关运算符的信息(例如重载),但似乎并不能很好地解释它们是什么。
什么是C++中的->*.*,与->.相比,您何时需要使用它们?

最佳答案

我希望这个例子能为您清除一切

//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();

现在,您不能使用x.somePointer()px->somePointer(),因为在类X中没有这样的成员。为此,使用了特殊的成员函数指针调用语法...您自己尝试一些示例,您就会习惯了

09-06 15:50