了解成员指针运算符
我从一本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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
问题是因为
op-> 处有额外的空格。 *pf
:我想@fefe可能已经在评论中说了原因。
->*
是单个运算符,类似于.*
。因此,如果将这两者分开,则会导致不同的语法,从而导致编译器错误。The problem is because of extra whitespace at
op-> *pf
: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.看一下指向类数据的指针。对于错误, ->* 是一个运算符,不能在它们之间放置空格。
Take a look at Pointer to class data. And for the error, ->* is an operator, you can't put a space between them.
iammilind 跟我打赌这个错误;
op->;必须更改 *pf
,以便将->*
一起作为单个运算符 - a 指向成员运算符的指针(无法找到更好的链接)。op ->* pf
中的空格完全有效。对于像
i++
这样的东西也是如此;++
是单个运算符,如果您尝试使用i++
,将会导致错误。现在来说说它正在做什么。该示例是一个指向成员函数的指针。
pf
被声明为class M
的成员函数,它接受两个int
参数,返回类型为void
。它被初始化为指向M::set_xy
函数。在
main
内部:n
的类型为M
,因此为了使用pf
来调用set_xy
ofn
您可以使用.*
运算符:(n.*pf)(10, 20);
。这相当于n.set_xy(10, 20);
。由于
op
的类型为M*
(指向M
对象的指针),因此您需要使用->*
运算符并调用pf
指向的函数为:(op->*pf)(30, 40);
,即相当于op->set_xy(30, 40);
内部
sum
:mx
和my
加在一起。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 inop ->* 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 havei+ +
.Now for what it's doing. The example is of a pointer to a member function.
pf
is being declared as a member function ofclass M
, that takes twoint
arguments with avoid
return type. It's being initialized to point to theM::set_xy
function.Inside
main
:n
is of typeM
, therefore in order to usepf
to callset_xy
ofn
you'd use the.*
operator:(n.*pf)(10, 20);
. That's equivalent ton.set_xy(10, 20);
.Since
op
is of typeM*
(a pointer to anM
object), you'll need to use the->*
operator and call the function pointed to bypf
as:(op->*pf)(30, 40);
, which is equivalent toop->set_xy(30, 40);
Inside
sum
:m.x
andm.y
using those types of pointers.