了解成员指针运算符

发布于 2024-12-21 18:33:54 字数 852 浏览 3 评论 0原文

我从一本c++练习书中复制了这个程序。幕后发生了什么?

预期输出是:

总和=30总和=70

#include<iostream>
using namespace std;

class M
{
    int x;
    int y;
public:
    void set_xy(int a, int b)
    {
        x=a;
        y=b;
    }
    friend int sum(M m);
};

int sum (M m);
//so far so good, problem begins from here. what's happening after here?
{                               
    int M ::*px = &M ::x;
    int M ::*py = &M ::y;
    M *pm =&m;
    int s= m.*px+ pm->*py;
    return s;
}

int main()
{
    M n;
    void (M :: *pf)(int, int) = &M ::set_xy;
    (n.*pf)(10, 20);
    cout <<"sum=" << sum(n) << endl;

    M *op= &n;
    (op-> *pf)(30,40);
    cout << "sum=" << sum(n)<< endl;

    cin.ignore();
    getchar();
    return 0;
}

I copied this program from a c++ practice book. What's going on behind the scenes?

The expected output is:

sum=30 sum=70

#include<iostream>
using namespace std;

class M
{
    int x;
    int y;
public:
    void set_xy(int a, int b)
    {
        x=a;
        y=b;
    }
    friend int sum(M m);
};

int sum (M m);
//so far so good, problem begins from here. what's happening after here?
{                               
    int M ::*px = &M ::x;
    int M ::*py = &M ::y;
    M *pm =&m;
    int s= m.*px+ pm->*py;
    return s;
}

int main()
{
    M n;
    void (M :: *pf)(int, int) = &M ::set_xy;
    (n.*pf)(10, 20);
    cout <<"sum=" << sum(n) << endl;

    M *op= &n;
    (op-> *pf)(30,40);
    cout << "sum=" << sum(n)<< endl;

    cin.ignore();
    getchar();
    return 0;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

此生挚爱伱 2024-12-28 18:33:54

问题是因为 op-> 处有额外的空格。 *pf

 (op->*pf)(30,40);  // ok

我想@fefe可能已经在评论中说了原因。 ->* 是单个运算符,类似于 .*。因此,如果将这两者分开,则会导致不同的语法,从而导致编译器错误。

The problem is because of extra whitespace at op-> *pf:

 (op->*pf)(30,40);  // ok

I think @fefe has probably said the reason in comment. ->* is a single operator, similar to .*. So, if those 2 are separated, then it will result in different syntax, which gives compiler error.

摇划花蜜的午后 2024-12-28 18:33:54

看一下指向类数据的指针。对于错误, ->* 是一个运算符,不能在它们之间放置空格。

Take a look at Pointer to class data. And for the error, ->* is an operator, you can't put a space between them.

始终不够 2024-12-28 18:33:54

iammilind 跟我打赌这个错误; op->;必须更改 *pf ,以便将 ->* 一起作为单个运算符 - a 指向成员运算符的指针(无法找到更好的链接)。 op ->* pf 中的空格完全有效。

对于像 i++ 这样的东西也是如此; ++ 是单个运算符,如果您尝试使用 i++,将会导致错误。

现在来说说它正在做什么。该示例是一个指向成员函数的指针。 pf 被声明为 class M 的成员函数,它接受两个 int 参数,返回类型为 void 。它被初始化为指向 M::set_xy 函数。

main 内部:

  • n 的类型为 M,因此为了使用 pf 来调用 set_xy of n 您可以使用 .* 运算符:(n.*pf)(10, 20); 。这相当于 n.set_xy(10, 20);

  • 由于 op 的类型为 M*(指向 M 对象的指针),因此您需要使用 ->* 运算符并调用 pf 指向的函数为:(op->*pf)(30, 40);,即相当于op->set_xy(30, 40);

内部 sum

  • 这些示例只是指向成员/实例的指针变量,而不是成员函数。它只是演示如何使用这些类型的指针将 mxmy 加在一起。

iammilind bet me to the error; op-> *pf must be changed so that you have ->* together as a single operator - a pointer to member operator (couldn't find a better link). The whitespace in op ->* pf is perfectly valid.

That's the same for something like i++; ++ is a single operator and will cause an error if you try and have i+ +.

Now for what it's doing. The example is of a pointer to a member function. pf is being declared as a member function of class M, that takes two int arguments with a void return type. It's being initialized to point to the M::set_xy function.

Inside main:

  • n is of type M, therefore in order to use pf to call set_xy of n you'd use the .* operator: (n.*pf)(10, 20);. That's equivalent to n.set_xy(10, 20);.

  • Since op is of type M* (a pointer to an M object), you'll need to use the ->* operator and call the function pointed to by pf as: (op->*pf)(30, 40);, which is equivalent to op->set_xy(30, 40);

Inside sum:

  • The examples are simply of pointers to member/instance variables, as opposed to member functions. It's simply demonstrating how you would add together m.x and m.y using those types of pointers.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文