tags:

views:

77

answers:

2

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)

+2  A: 

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));
}
Adam
Thanks Adam! This is what I do right now. I wonder if it is possible to make the compiler do the work. Something like "if you want to go there, go over there first".
Markus
I understand. Although this solution seems most direct to me, I added another possibility in another answer.
Adam
Go with free functions. Implicit conversions are generally a bad way to go, even when they're posssible.
jalf
The problem is that I need to add some sort of indirection to the scripting system that takes care of the conversion which makes "manual" conversion hard to use (unfortunately).Anyway, I accept this as the answer - thanks Adam and jalf!
Markus
+1  A: 

Your statement in the question is correct. The type conversion operator needs to be a non-static member. If you really want conversion-type semantics, you could extend each of those classes for use in your application code:

// header:
class ConvertibleVector3d;

ConvertibleBtVector : public btVector3
{
  operator ConvertibleVector3d() const;
}

ConvertibleVector3d : public vector3d<float>
{
  operator ConvertibleBtVector() const;
}

//impl:
ConvertibleBtVector::operator ConvertibleVector3d() const
  {
    ConvertibleVector3d retVal;
// convert this into retVal...
    return retVal;
  }

ConvertibleVector3d::operator ConvertibleBtVector() const;
  {
    ConvertibleBtVector retVal;
// convert this into retVal...
    return retVal;
  }

void f(void)
{
  ConvertibleVector3d one;   
// ...populate...   
  ConvertibleBtVector two(one);    
// ...
  ConvertibleVector3d three;
  three = two;
}

The names are a bit verbose but hopefully the intent is clear.

The public inheritance means you should be able to use instances of these classes in the same way as the base class, except they will be assignable and constructable from each other. Of course this couples the two classes, but that may be acceptable since it sounds like that's what your application intends to do anyway.

Adam
Thanks again! I accepted the other one as the answer. This one feels more natural, but since it's not always possible to modify library code (although in this case it is) ... and so on. :)
Markus