我有两个结构在给定帧中定义一个点和一个向量。
struct point3D
{
float x;
float y;
float z;
};
struct vector3D
{
float x;
float y;
float z;
};
它们被定义为两个不同结构的原因是,还有其他函数将点(point3D
(与向量(vector3D
(不同,因为它们具有相同类型的成员变量
我想知道是否有办法将其中一个类型转换为另一个说法,例如:
point3D variable1;
vector3D variable2;
variable2=(vector3D)variable1;
你可以这样做
struct vector3D
{
float x;
float y;
float z;
};
struct point3D
{
float x;
float y;
float z;
explicit operator vector3D() {
return {x, y, z};
}
};
我会把刀给你,尽管我确信你不是浪涌。卡斯滕建议 - 在评论中 - 从向量中导出点。如果这不好,请继续作弊:正确的铸造运算符是reinterpret_cast
.
point3D variable1;
vector3D variable2;
variable2=reinterpret_cast<vector3D&>(variable1);
但那是C++。如果您需要 C 样式强制转换,那么指针强制转换就是这样:
variable2=*(vector3D*)(void*)&variable1;
不建议使用上述两种解决方案中的任何一种。我会考虑重新设计,如果我在你的鞋子里。