“指向成员的指针”返回类型的语法是什么?
struct Point {
int x;
int y;
int z;
};
// compiles
decltype(&Point::x) getX () {
return &Point::x;
}
// does not compile... how to do this without decltype?
int (Point::*) getX () {
return &Point::x;
}
// Use case:
Point p, q;
auto pm = getX();
p.*pm = 1; // p.x = 1
q.*pm = 2; // q.x = 2
最佳答案
你不需要括号:
int Point::* getX () {
return &Point::x;
}
想象一下将括号与常规指针一起使用:
int (*) getX() // compiler says WTF
{
static int x;
return &x;
}
关于C++返回指向成员的指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24917238/