Translating C++ code, syntax question

I have a point class (much like the MFC::CPoint, but double precision) in C++ that allows me to do a lot of geometry calculations quickly.  I often use an expression like:

CdPoint A = CdPoint( dX1, dY1 ) + CdPoint( dR*Math.Cos(dAng), dR*Math.Sin(dAng) );

However, I find that C# doesn't seem to like that type of expression.  Neither does C++/CLR.  I have been able to get the following to compile:

CdPoint B = new CdPoint( dX1, dY1 );
CdPoint C = new CdPoint( dR*Math.Cos(dAng), dR*Math.Sin(dAng) );
CdPoint A = B + C;

But I really miss the simplicity of the C++/Native syntax.

I have tried defining it as a struct instead of a class, which changes a lot of the syntax but doesn't solve this issue.

Is there a different way of doing this that allows me to use something like the old C++/Native syntax

Struggling C++ programmer.


Answer this question

Translating C++ code, syntax question

  • RMJR

    The disconnect that you're experiencing is due to the fact that C++ is allocating CdPoint objects on the stack when you declare them as CdPoint(dX1, dY1). In .NET, structs are stack allocated (assuming you're dealing with local variables), but the syntax of C# still requires you to use the "new" keyword. The same is true if they're classes and you're dealing with instances. The best you can do is:

    CdPoint A = new CdPoint( dX1, dY1 ) + new CdPoint( dR*Math.Cos(dAng), dR*Math.Sin(dAng) );

  • Translating C++ code, syntax question