C++ 中的 ->* 运算符是什么?

发布于 2024-08-11 22:29:16 字数 758 浏览 3 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

冰魂雪魄 2024-08-18 22:29:16

就像 .* 一样,->* 与指向成员的指针一起使用。 C++ FAQ LITE 有一整节专门介绍指针-致会员。

#include <iostream>

struct foo {
    void bar(void) { std::cout << "foo::bar" << std::endl; }
    void baz(void) { std::cout << "foo::baz" << std::endl; }
};

int main(void) {
    foo *obj = new foo;
    void (foo::*ptr)(void);

    ptr = &foo::bar;
    (obj->*ptr)();
    ptr = &foo::baz;
    (obj->*ptr)();
    return 0;
}

Just like .*, ->* is used with pointers to members. There's an entire section on C++ FAQ LITE dedicated to pointers-to-members.

#include <iostream>

struct foo {
    void bar(void) { std::cout << "foo::bar" << std::endl; }
    void baz(void) { std::cout << "foo::baz" << std::endl; }
};

int main(void) {
    foo *obj = new foo;
    void (foo::*ptr)(void);

    ptr = &foo::bar;
    (obj->*ptr)();
    ptr = &foo::baz;
    (obj->*ptr)();
    return 0;
}
鸵鸟症 2024-08-18 22:29:16

重载的 ->* 运算符是二元运算符(而 .* 不可重载)。它被解释为普通的二元运算符,因此在您最初的情况下,为了调用该运算符,您必须执行类似的操作,

A a;
B* p = a->*2; // calls A::operator->*(int)

您在 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 like

A a;
B* p = a->*2; // calls A::operator->*(int)

What 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.

吻安 2024-08-18 22:29:16

与任何其他运算符一样,您也可以显式调用它:

a.operator->*(2);

Like any other opperator, you can also call it explicitly:

a.operator->*(2);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文