【发布时间】:2020-11-26 18:54:33
【问题描述】:
所以我一直在看一些东西,发现这个帖子Aliasing struct and array the C++ way
,这是问题的答案
#include <math.h>
struct Point {
double x;
double y;
double z;
};
double dist(struct Point *p1, struct Point *p2) {
constexpr double Point::* coords[3] = {&Point::x, &Point::y, &Point::z};
double d2 = 0;
for (int i=0; i<3; i++) {
double d = p1->*coords[i] - p2->*coords[i];
d2 += d * d;
}
return sqrt(d2);
}
现在我的问题是我不知道是什么
constexpr double Point::* coords[3] = {&Point::x, &Point::y, &Point::z};
应该这样做...
我知道constexpr 使它成为在编译时定义的常量,并且显然使用了double,因为该结构包含双精度,但Point::* 和{&Point::x, &Point::y, &Point::z}; 让我感到困惑。首先是什么是Point::*?我猜 * 表示它是某种指针,但指向什么?以及这些地址{&Point::x, &Point::y, &Point::z}?
此整个表达式究竟是什么定义?
【问题讨论】: