473,386 Members | 1,864 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Overloaded unary negation operator

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.

Nov 17 '05 #1
1 1119
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.

Nov 17 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Dave Theese | last post by:
Am I correct in saying that it is not possible to overload unary + and unary -?
7
by: John J | last post by:
To write a unary operator- operation that returns the negative of a value is it just as simple as subtracting the value from itself? ie. { return (value - value); } Thanks
20
by: Brad Eck | last post by:
"The only operators that cannot be overloaded are :: (scope resolution), . (member selection), and .* (member selection through pointer to function). Quoting from Stroustrup's 3rd edition of _The...
17
by: joshc | last post by:
I searched through the newsgroup for this and found the answer but wanted to make sure because of something that came up. I want the absolute value of a 'short int' so to avoid the dangers of...
2
by: Javier Estrada | last post by:
1. For types smaller than int, when I compile: class MyClass { static void Main(string args) { x = 10; y = -x; }
13
by: Marc | last post by:
Hi, I've been lurking on clc for a few months now, and want to start by thanking the regulars here for opening my eyes to a whole new dimension of "knowing c". Considering I had never even...
3
by: ES Kim | last post by:
iterator classes provide overloaded operators like this in general: template <typename T> class Iterator { Iterator operator++(int); // postfix ++ T& operator*(); }; Iterator<int> i;
6
by: Angel Tsankov | last post by:
How can an overloaded operator& take the address of its argument: template<typename T> Smth operator &(T& SomeObject) { // The address of SomeObject is needed here }
16
by: JoseMariaSola | last post by:
How may operators and operands does (typename) expression has? I'd say one operator, the cast operator, and two operands: typename and expression. But everywhere I read, cast is categorized as...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.