Overloaded unary negation operator

H

Heloise

Hi all,

I'm trying to write an overloaded operator for unary negation e.g.,

MyClass a(5);
MyClass b(0);
b = -a;

Here is my class implementation:

public ref class Angle
{
double radianValue; // value of the angle in radians

Angle():radianValue(0.0){};

Angle(double radians):radianValue(radians){};

Angle^ operator-()
{
return gcnew Angle(-radianValue);
}
}

My C# unit test code:

Angle angle1(10.0);
Angle angle2;
angle 2 = -angle1;

gives this error:

error CS0023: Operator '-' cannot be applied to operand of type 'Angle'

My other operators work fine.

Any ideas? Thanks in advance.
 
H

Heloise

Hi all,

OK, I figured it out, so I thought I'd share it with you:

static Angle^ operator-(const Angle^ source)
{
return gcnew Angle(-source->radianValue);
}

Works just fine - the C# code needs all C++/CLI operators to be static.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top