Sign in to follow this  
hplus0603

[.net] Returning a ref struct in C++/CLR

Recommended Posts

Here's some code:
namespace fluff
{
	public ref struct Vector3
	{
	  float X;
	  float Y;
	  float Z;
	  Vector3(float x, float y, float z)
	  {
	    X = x;
	    Y = y;
	    Z = z;
	  }
	  Vector3 operator+(Vector3 b)
	  {
	    Vector3 ret(X + b.X, Y + b.Y, Z + b.Z);
	    return ret;
	  }
	};
}
I get error C2440 on the operator+ line, saying I can't convert a Vector3 to a Vector3 ... So, how do I return instance of value types?

Share this post


Link to post
Share on other sites
Just to explain further, for ref types you have to move them around -- returns mainly -- using handles (Vector3^). In your case, the parameter would have actually been Vector3^ and gained stack semantics, which is an entirely new form of crazy.

Oh and honestly, you're much better off simply taking the SlimDX code for this stuff (MIT license). We've done all the stupid legwork, please don't be shy in leveraging it.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this