Jump to content
  • Advertisement

Archived

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

Carradine

creating operators for an existing class

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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

}
};

However, there are several things non-ideal about this:
- 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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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();

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!