简体   繁体   English

使用 static_cast 投射 POD 对象

[英]Casting POD object with static_cast

this is what i mean,这就是我的意思,

class V3
{
public:
float x,y,z;
};

class V3_
{
 public:
float x,y,z;
};

V3_ vec1;
V3 vec2 = static_cast<V3_>(vec1);

what should i do to make this cast work in compile time?我应该怎么做才能使这个演员在编译时工作?

Thanks.谢谢。

edit:编辑:

there seems to be some misunderstanding about what i want to do.似乎对我想做的事情有些误解。 i can write an operator to the conversion, but i want it to be done in compile time.我可以为转换编写一个运算符,但我希望它在编译时完成。

here are two implementations of Vector3 that allows static_cast.这是允许 static_cast 的 Vector3 的两个实现。 i dont know how does it work, but it does work.我不知道它是如何工作的,但它确实有效。

http://www.ogre3d.org/docs/api/html/OgreVector3_8h_source.html http://www.ogre3d.org/docs/api/html/OgreVector3_8h_source.html

http://harry-3d-engine.googlecode.com/svn/trunk/NxOgreVec3.hhttp://harry-3d-engine.googlecode.com/svn/trunk/NxOgreVec3.h

i can do this cast我可以做这个演员

 void DynamicBody::SetLinearMomentum(const Vector3& vel)
 {
body->setLinearMomentum(static_cast<NxOgre::Vec3>(vel));
 }

i would like an explanation on how i can do this.我想解释一下我如何做到这一点。

edit:编辑:

upon further investigation i found how it actually does this, it calls经过进一步调查,我发现它实际上是如何做到这一点的,它调用

template<class user_xyz_vector_type>
inline user_xyz_vector_type as() const { ... }

that is not an actual static_cast, but compiler accepts it as one.这不是一个实际的 static_cast,但编译器将其作为一个接受。 i really hoped casting pods to eachother was an actual thing.我真的希望向彼此投射豆荚是一件真实的事情。

This cast will not work at any time.此演员表在任何时候都不起作用。 You can't static_cast completely unrelated and not convertible types to each other.你不能static_cast完全不相关且不能相互转换的类型。

I'm not sure what you are trying to do, so I don't know what the work "work" means in this case, but at a raw memory level the conversion can be performed by reinterpret_cast我不确定你要做什么,所以我不知道在这种情况下工作“工作”是什么意思,但在原始内存级别,可以通过reinterpret_cast执行转换

V3 vec2 = reinterpret_cast<V3_&>(vec1);

but this is an ugly hack that is not guaranteed to work, since, again, your types are unrelated.但这是一个丑陋的黑客,不能保证工作,因为,同样,你的类型是不相关的。 With the same degree (or even better) of success you can simply memcpy one object into another.随着成功的程度相同(或更好),你可以简单memcpy一个对象到另一个。

You could avoid using cast altogether as:您可以完全避免使用强制转换,因为:

V3 vec2 = {vec1.x, vec1.y, vec1.z};

Or write conversion function:或者写转换函数:

V3 to_V3(V3_ const & v)
{
   V3 v3 = {v.x, v.y, v.z};
   return v3;
}

and use it as:并将其用作:

V3 vec2 = to_V3(vec1);

The types are unrelated, therefore you need reinterpret_cast :类型不相关,因此您需要reinterpret_cast

V3_ vec1;
V3 vec2 = reinterpret_cast<V3_&>(vec1);
V3_ vec1;
V3 vec2 = *(V3*)&vec1;

EDIT: still, you know that's probably not a good thing to do?编辑:仍然,你知道这可能不是一件好事吗?

To static_cast V3 to V3_ use public: on V3 class declaration.static_cast V3V3_使用public:V3类声明上。 class V3 : public V3_ . class V3 : public V3_ This way you can convert objects.这样您就可以转换对象。 Use virtual deconstructors if you want polymorphism and you're aware of what you're doing.如果您想要多态性并且知道自己在做什么,请使用虚拟解构器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM