gpt4 book ai didi

c++ - 通过增量运算符访问类成员

转载 作者:太空狗 更新时间:2023-10-29 23:06:01 29 4
gpt4 key购买 nike

我有一个类,例如:

class Vector
{
float x, y, z
};
Vector v;

还有一个指针:

float *c = &v.x;

当我使用增量运算符访问 y 和 z 成员时,它会正常工作吗?

附言用这种方式做的风格不好,但它是运动兴趣。

最佳答案

Will it be works correctly, when I'll use increment operator for access to y and z members?

没有。未定义的行为。您不能跨对象执行指针运算,也不能保证结构填充。

不过你可以这样做:

class Vector
{
float v[3];
int& x() { return v[0]; }
int x() const { return v[0]; }
// and so on ...
};

关于c++ - 通过增量运算符访问类成员,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17097137/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com