Archived

This topic is now archived and is closed to further replies.

creating operators for an existing class

This topic is 5931 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

I have created a simple class called POSITION2, it is simply a class like this: class POSITION2 { ...several operators int x; int y; } What I want to do is be able to make an operator= function so I can convert POSITION2 into D3DXVECTOR2. So i can do: POSITION2 temp2(3,4); D3DXVECTOR2 temp = temp2; //dont know how The problem is I cant just go into the D3DXVECTOR2 class and insert the operator= code obviously. I have already created the code to do POSITION2 temp = D3DXVECTOR2(3.0f,4.0f); since I can do that in my own POSITION2 class, but I do not how to reverse the process like in the example above, since D3DXVECTOR2 is a "set" file and cannot be modified. Thanks for any help you can give me. Carradine

Share on other sites
I have basically no experience in this matter, but have you tried using the "friend" keyword infront of your function/overloaded operator?

Share on other sites
You could use a cast-operator:

class POSITION2{  //several operators  int x;  int y;  operator D3DXVECTOR2 ()  {    return something;  }}POSITION2 temp2(3,4);D3DXVECTOR2 temp = temp2; //dont know how

If I recall corectly conversion should happen implicitly, otherwise you''d have to add a typecast.

Share on other sites
A solution is to create a conversion operator for POSITION2:

  class Position2{public:  // tricky syntax, watch the declaration here..  operator D3DXVECTOR2 () const  {    D3DXVECTOR2 retval;    // initialize retval with contents of this object    return retval; // conversion operators implicitly return                   // the conversion type  }};

- it returns a D3DXVECTOR2 by value, as it must. Return value optimization might work around this.
- conversion operators can be called at unexpected times; some might think having conversion operators obfuscates code or creates unexpected results. This is why there is no operator const char * for std::string; you''re explicitly required to call string::c_str () instead.
- creates a tight physical dependency of POSITION2 on D3DXVECTOR2.

For these reasons, I''d rather see a static function in a utility class (or free function in a utility namespace, for you purists) that performs this task:

  namespace Converters{  D3DXVECTOR2 cvtP2ToD3DXV2 (const POSITION2 &pos);};

And, um...think of a better function name.

Share on other sites
I am already using a conversion function, I just wanted to attempt to not use one, and find a way to overload operator= to convert the type instead, if that is the most acceptable and safe way to do it then I suppose I would have to stick to that usage.

Share on other sites
You can just overload the = operator.

DXBLAHBLAH operator=(DXBLAHBLAH, POSITION2)

This is a ''free function'', independent of either class.
This will not interfere with the = operator on DXBLAHBLAH because of simple function overloading. The compiler interprets this function the same as ''DXBLAHBLAH DXBLAHBLAH::operator=(POSITION2)''.

Share on other sites
I''m fairly certain that operator= has to be overload in the class that it operates on - it can''t be a friend function.

That means you''d have to modify D3DXVECTOR2 to make it compatible with Position2.

The safest way (from the using Position2 stand-point) is to add an explicit function to convert a Position2 into a D3DXVECTOR2:

  struct Position2{D3DXVECTOR2& d3dxvector2(){return *reinterpret_cast<D3DXVECTOR2*>(this);}const D3DXVECTOR2& d3dxvector2() const{return *reinterpret_cast<D3DXVECTOR2*>(this);}};Position2 p;D3DXVECTOR2 v = p.d3dxvector2();

1. 1
2. 2
Rutin
31
3. 3
4. 4
5. 5
khawk
14

• 11
• 11
• 23
• 10
• 9
• Forum Statistics

• Total Topics
633647
• Total Posts
3013108
×