Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
701 views
in Technique[技术] by (71.8m points)

c++ - Non-member conversion functions; Casting different types, e.g. DirectX vector to OpenGL vector

I am currently working on a game "engine" that needs to move values between a 3D engine, a physics engine and a scripting language. Since I need to apply vectors from the physics engine to 3D objects very often and want to be able to control both the 3D, as well as the physics objects through the scripting system, I need a mechanism to convert a vector of one type (e.g. vector3d<float>) to a vector of the other type (e.g. btVector3). Unfortunately I can make no assumptions on how the classes/structs are laid out, so a simple reinterpret_cast probably won't do.

So the question is: Is there some sort of 'static'/non-member casting method to achieve basically this:

vector3d<float> operator vector3d<float>(btVector3 vector) {
    // convert and return
}

btVector3 operator btVector3(vector3d<float> vector) {
    // convert and return
}

Right now this won't compile since casting operators need to be member methods. (error C2801: 'operator foo' must be a non-static member)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I would suggest writing them as a pair of free functions (i.e. don't worry about making them 'operators'):

vector3d<float> vector3dFromBt(const btVector3& src) {
    // convert and return
}

btVector3 btVectorFrom3d(const vector3d<float>& src) {
    // convert and return
}

void f(void)
{
  vector3d<float> one;   
// ...populate...   
  btVector3 two(btVectorFrom3d(one));    
// ...
  vector3d<float> three(vector3dFromBt(two));
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...