473,398 Members | 2,113 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,398 software developers and data experts.

CLS complaint = Operators should not be overloaded

http://www.devarticles.com/c/a/C-Sha...CLS-Compliant/

Does anyone follow this? I know it's good to not have to deal with
unsigned types in the interface, so that's great. But CLS compliance
is quite strict:

"Only properties and methods may be overloaded, Operators should not
be overloaded."

Does that mean I can't make my own + operator for my own object? I
find this hard to believe. But, in general, it seems that CLS
compliant code is a very good idea. I wonder how widespread its usage
is? Do you guys use it?

Zytan

Mar 9 '07 #1
4 2325
Zytan wrote:
http://www.devarticles.com/c/a/C-Sha...CLS-Compliant/

Does anyone follow this? I know it's good to not have to deal with
unsigned types in the interface, so that's great. But CLS compliance
is quite strict:

"Only properties and methods may be overloaded, Operators should not
be overloaded."

Does that mean I can't make my own + operator for my own object? I
find this hard to believe. But, in general, it seems that CLS
compliant code is a very good idea. I wonder how widespread its usage
is? Do you guys use it?
The short answer is that not every .NET language supports operator
overloading so it is not part of the CLS. That doesn't mean you can't
overload operators in your code, but if you do you need to mark that section
as CLSComplaint(false) and give an alternate way of doing the same thing for
..NET languages that do not support operator overloading. For example, if
you overload + then also give your class an Add method that does the same
thing.

As far as CLS compliant code being a very good idea, that is only true if
you plan on making your objects available to any .NET language. If you know
your consumers are all going to be written in languages that support
operator overloading, and it makes sense in your class to do that, then you
shouldn't necessarily feel restricted by CLS compliance.
--
Tom Porterfield

Mar 9 '07 #2
Thus wrote Zytan,
http://www.devarticles.com/c/a/C-Sha...CLS-Compliant/

Does anyone follow this? I know it's good to not have to deal with
unsigned types in the interface, so that's great. But CLS compliance
is quite strict:

"Only properties and methods may be overloaded, Operators should not
be overloaded."
That is not correct. If you overload an operator, you *should* provide an
alternative method that fulfills the same purpose. This keeps your type fully
usable for .NET languages that don't support operator overloading (see System.DateTime
for example).

Cheers,
--
Joerg Jooss
ne********@joergjooss.de
Mar 9 '07 #3
The short answer is that not every .NET language supports operator
overloading so it is not part of the CLS. That doesn't mean you can't
overload operators in your code, but if you do you need to mark that section
as CLSComplaint(false) and give an alternate way of doing the same thing for
.NET languages that do not support operator overloading. For example, if
you overload + then also give your class an Add method that does the same
thing.
Oh, ok.
As far as CLS compliant code being a very good idea, that is only true if
you plan on making your objects available to any .NET language. If you know
your consumers are all going to be written in languages that support
operator overloading, and it makes sense in your class to do that, then you
shouldn't necessarily feel restricted by CLS compliance.
Wonderful explanation, Tom. Thanks!

Zytan

Mar 9 '07 #4
"Only properties and methods may be overloaded, Operators should not
be overloaded."

That is not correct. If you overload an operator, you *should* provide an
alternative method that fulfills the same purpose. This keeps your type fully
usable for .NET languages that don't support operator overloading (see System.DateTime
for example).
Great, and this totally makes sense. So, I can do it, and people who
use the more powerful languages can take advantage of it, but for
those without such features, they can still use my unit!

Thanks, Joerg!

Zytan

Mar 9 '07 #5

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

Similar topics

5
by: Andy Jarrell | last post by:
I'm trying to inherit from a specific class that has an overloaded operator. The problem I'm getting is that certain overloaded operators don't seem to come with the inheritance. For example: ...
3
by: Nimmi Srivastav | last post by:
There's a rather nondescript book called "Using Borland C++" by Lee and Mark Atkinson (Que Corporation) which presents an excellent discussion of overloaded new and delete operators. I am...
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...
7
by: bberu | last post by:
Hi, I know it is possible to refine operators (like +, -, * or /) on classes. But is it possible on simple types ? ex : If I have a type like : typedef int vector is it possible to redefine a...
4
by: masood.iqbal | last post by:
Please help me with this doubt that I have regarding overloaded operators. Sometimes they are member functions and sometimes they are friends (e.g. see the code snippet from Stroustrup, Second...
10
by: maadhuu | last post by:
hi i wasnt to know the answer for the following. now ,u can overload all the operators which are basically determined at runtime (coz' of whch operators like sizeof())cannot be overloaded. now...
0
by: Syanide | last post by:
here's a bit info for you fellas: eg.. you have Square classes u wanna add public static int operator+ (Square s1, Square s2) { // your code here } comparision operators..
1
by: Alex Zhitlenok | last post by:
Hi, My question is how to resolve in C# ambiguous overloaded operators? Let say, I have two unrelated classes A and B, each one implements overloaded operator + with the first parameter of type...
3
by: Silver Phoenix | last post by:
Greetings. I have some questions that I would like to clear out. 1. Why do operators have to be static? (This question relates to the language specification) 2. More importantly, couldn't...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.