C++ 中的 ->* 运算符是什么?
C++ 继续给我带来惊喜。 今天我发现了 ->* 运算符。它是可重载的,但我不知道如何调用它。我设法在课堂上超载它,但我不知道如何调用它。
struct B { int a; };
struct A
{
typedef int (A::*a_func)(void);
B *p;
int a,b,c;
A() { a=0; }
A(int bb) { b=b; c=b; }
int operator + (int a) { return 2; }
int operator ->* (a_func a) { return 99; }
int operator ->* (int a) { return 94; }
int operator * (int a) { return 2; }
B* operator -> () { return p; }
int ff() { return 4; }
};
void main()
{
A a;
A*p = &a;
a + 2;
}
编辑:
感谢您的回答。要调用我编写的重载函数
void main()
{
A a;
A*p = &a;
a + 2;
a->a;
A::a_func f = &A::ff;
(&a->*f)();
(a->*f); //this
}
C++ continues to surprise me.
Today i found out about the ->* operator. It is overloadable but i have no idea how to invoke it. I manage to overload it in my class but i have no clue how to call it.
struct B { int a; };
struct A
{
typedef int (A::*a_func)(void);
B *p;
int a,b,c;
A() { a=0; }
A(int bb) { b=b; c=b; }
int operator + (int a) { return 2; }
int operator ->* (a_func a) { return 99; }
int operator ->* (int a) { return 94; }
int operator * (int a) { return 2; }
B* operator -> () { return p; }
int ff() { return 4; }
};
void main()
{
A a;
A*p = &a;
a + 2;
}
edit:
Thanks to the answer. To call the overloaded function i write
void main()
{
A a;
A*p = &a;
a + 2;
a->a;
A::a_func f = &A::ff;
(&a->*f)();
(a->*f); //this
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
就像
.*
一样,->*
与指向成员的指针一起使用。 C++ FAQ LITE 有一整节专门介绍指针-致会员。Just like
.*
,->*
is used with pointers to members. There's an entire section on C++ FAQ LITE dedicated to pointers-to-members.重载的
->*
运算符是二元运算符(而.*
不可重载)。它被解释为普通的二元运算符,因此在您最初的情况下,为了调用该运算符,您必须执行类似的操作,您在 Piotr 的答案中读到的内容适用于内置运算符,而不是你的超载的。您在添加的示例中调用的也是内置运算符,而不是重载的运算符。为了调用重载运算符,您必须执行我在上面的示例中所做的操作。
The overloaded
->*
operator is a binary operator (while.*
is not overloadable). It is interpreted as an ordinary binary operator, so in you original case in order to call that operator you have to do something likeWhat you read in the Piotr's answer applies to the built-in operators, not to your overloaded one. What you call in your added example is also the built-in operator, not your overloaded one. In order to call the overloaded operator you have to do what I do in my example above.
与任何其他运算符一样,您也可以显式调用它:
Like any other opperator, you can also call it explicitly: