473,738 Members | 5,934 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Why can't operator overloads be abstract?

This code won't compile because "the modifier 'abstract' is not valid for
this item".

abstract class X
{
public abstract static bool operator >(X a, X b);
public abstract static bool operator <(X a, X b);
}

Why is that? -- apart from the obvious reason that the specification doesn't
support it. It seems like a reasonable contract to be able to enforce on
derived classes.

Similarly, interfaces can't have operator overloads. Why is *that*?

(For anybody who's curious, I read Stepanov's comment that "you can't write
a generic max() in Java that takes two arguments of some type and has a
return value of that same type", and I was messing about trying to do it in
C# 2.0. I haven't managed it yet.)

Eq.
Jun 27 '08 #1
8 3750
Paul E Collins wrote:
This code won't compile because "the modifier 'abstract' is not valid for
this item".

abstract class X
{
public abstract static bool operator >(X a, X b);
public abstract static bool operator <(X a, X b);
}

Why is that? -- apart from the obvious reason that the specification doesn't
support it. It seems like a reasonable contract to be able to enforce on
derived classes.

Similarly, interfaces can't have operator overloads. Why is *that*?

(For anybody who's curious, I read Stepanov's comment that "you can't write
a generic max() in Java that takes two arguments of some type and has a
return value of that same type", and I was messing about trying to do it in
C# 2.0. I haven't managed it yet.)

Eq.

operators are static, and hence can't be virtual, nor abstract, it's
just the way they are implemented.

Generic max:

public static T Max<T>(T a, T b) where T: IComparable<T>
{
if (a == null) return b;
if (b == null) return a;
if (a.CompareTo(b) >= 0)
return a;
else return b;
}

--
Lasse Vågsæther Karlsen
mailto:la***@vk arlsen.no
http://presentationmode.blogspot.com/
PGP KeyID: 0xBCDEA2E3
Jun 27 '08 #2
Paul E Collins wrote:
This code won't compile because "the modifier 'abstract' is not valid for
this item".

abstract class X
{
public abstract static bool operator >(X a, X b);
public abstract static bool operator <(X a, X b);
}

Why is that? -- apart from the obvious reason that the specification doesn't
support it. It seems like a reasonable contract to be able to enforce on
derived classes.

Similarly, interfaces can't have operator overloads. Why is *that*?
If you really want to know, then send an email to Anders Hejlsberg
and ask.

But if I were to make a guess.

C++ support operator overloads both as an instance member and
as a friend function.

MS wanted C# to be simpler than C++, so one of them has to go.

Some people (including me) prefer the friend function for operators
like +-*/.

And my guess is that those designing C# had the same preference
and translated C++ friend function to C# static method.

Arne
Jun 27 '08 #3
Lasse Vågsæther Karlsen wrote:
Paul E Collins wrote:
>(For anybody who's curious, I read Stepanov's comment that "you can't
write a generic max() in Java that takes two arguments of some type
and has a return value of that same type", and I was messing about
trying to do it in C# 2.0. I haven't managed it yet.)
Generic max:

public static T Max<T>(T a, T b) where T: IComparable<T>
{
Maybe we are missing something.

Because the same thing can be done in Java.

The syntax is just:

public static <T extends Comparable<? super T>T Max(T a, T b) {

Arne
Jun 27 '08 #4
"Lasse Vågsæther Karlsen" <la***@vkarlsen .nowrote:
public static T Max<T>(T a, T b) where T: IComparable<T>
Ah, yes, it seems that I temporarily forgot about "where" on methods. Good
stuff.

If I'd thought a bit harder, I would have also recalled that statics can
never be inherited (so can't occur in an abstract class), but since I almost
never overload operators I didn't add the "static" until the compiler told
me to, and then I wasn't thinking about the effect on inheritance.

I'm not very familiar with Java, but if (as Arne says) the same construct is
possible then I assume that Stepanov was talking about an earlier version.

Eq.
Jun 27 '08 #5
Hi Paul,
If I'd thought a bit harder, I would have also recalled that statics can
never be inherited (so can't occur in an abstract class),
What do you mean when you say that statics methods are never inherited?
Looking at the code below, would you consider class "Bar" to be "inheriting "
the satic method from class "Foo"?

abstract class Foo
{
public static void DoIt()
{
}
}

abstract class Bar : Foo
{
}

class Program
{
static void Main(string[] args)
{
Bar.DoIt();
}
}

I really don't know what I am talking about here but .. I tried to come up
with a technical limitation that would explain the reason of why static
methods can't be overridden but was unable to come up with one. My guess is
that Microsoft just does not want to or has not goten around to implement
such feature but then again, I am probably missing something.

Cheers.

Jun 27 '08 #6
On Tue, 27 May 2008 19:01:40 -0700, Rene <a@b.comwrote :
[...]
I really don't know what I am talking about here but .. I tried to come
up with a technical limitation that would explain the reason of why
static methods can't be overridden but was unable to come up with one.
In spite of them being described in this thread and others?

Static methods in C# are always resolved at compile-time. Virtual methods
rely on a v-table (or at least something like that) to provide run-time
polymorphism, and in C# the only place it keeps a v-table is with an
object instance. No instance, no v-table. Without the v-table, there's
just no way to do it.

Even ignoring the implementation detail, it's a matter of C# semantics.
There would be no point in allowing virtual static methods, because you
are always required to supply the class name when calling a static
method. Because of that, you always know which class you're dealing
with. Polymorphism isn't relevant, because you never have code using
static methods that looks like it's working on one class when it's in fact
working on some other class. At compile time, you always know what class
it is.

It's not a question of "Microsoft doesn't want to" or "hasn't gotten
around to it". It's a fundamental part of the design of the language.

Pete
Jun 27 '08 #7
Paul,

So?
snip
(For anybody who's curious, I read Stepanov's comment that "you can't
write a generic max() in Java that takes two arguments of some type and
has a return value of that same type
snip

Cobol uses

If (A == x or y) to evaluate if A is equal to the value of x or the value
of y,

Has that to be in C# too?

Cor
"Paul E Collins" <fi************ ******@CL4.orgs chreef in bericht
news:Bb******** *************** *******@bt.com. ..
This code won't compile because "the modifier 'abstract' is not valid for
this item".

abstract class X
{
public abstract static bool operator >(X a, X b);
public abstract static bool operator <(X a, X b);
}

Why is that? -- apart from the obvious reason that the specification
doesn't support it. It seems like a reasonable contract to be able to
enforce on derived classes.

Similarly, interfaces can't have operator overloads. Why is *that*?

(For anybody who's curious, I read Stepanov's comment that "you can't
write a generic max() in Java that takes two arguments of some type and
has a return value of that same type", and I was messing about trying to
do it in C# 2.0. I haven't managed it yet.)

Eq.

Jun 27 '08 #8
"Cor Ligthert[MVP]" <no************ @planet.nlwrote :
Cobol uses
If (A == x or y) to evaluate if A is equal to the value of x or the value
of y,
Has that to be in C# too?
Irrelevant, because that's just a matter of syntax -- not something that
can't be stated in the language.

As Lasse showed, though, the generic max *is* possible in C#. I never said
it wasn't, only that I hadn't managed it yet.

Eq.
Jun 27 '08 #9

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

Similar topics

5
2077
by: bsaucer | last post by:
I am creating a class with operator overloads. It makes references to another class I've created, but do not wish to modify. I try to overload an operator having arguments having the other class type, but none of the new class type. However it does return a type of the new class. The compiler tells me that at least one parameter must be of the containing type. Is there a way to do this? The other class hos no reference to this new class. Is...
4
1308
by: Harlan Messinger | last post by:
Since operator overloads into static functions in C#, there really doesn't need to be a connection between the types to which the operator is being applied and the type in which the overload is defined, does there? I mean, there's nothing to prevent the following, right? public class A { public static B operator + (C c, D d) { ... } ... }
26
21335
by: Paul | last post by:
public class A { public A () { // here I would like to call the second version of _ctor, how to accomplish this ? } public A (int a, int b, int c) {
17
2509
by: Chris | last post by:
To me, this seems rather redundant. The compiler requires that if you overload the == operator, you must also overload the != operator. All I do for the != operator is something like this: public static bool operator !=(MyType x, MyType y) { return !(x == y); } That way the == operator handles everything, and extra comparing logic isn't
2
1466
by: David | last post by:
i've abstract class Weight public abstract class Weight { public int Quantity; // how much it weigths } and then i created 3 derived classes: Kilogram, Gram and Milligram in each of them i created an implicit operator to convert from one to another...
59
3453
by: Michael C | last post by:
eg void DoIt() { int i = FromString("1"); double d = FromString("1.1"); } int FromString(string SomeValue) {
3
1444
by: Jesper | last post by:
Hi, I've made a class with the following operator overloads listed below. However, If I test a 'pointer' for an instace, I get an exception that the object is not set to an insstance of an object. ID id = new ID(1,2) ..... e.g. if ( id != null ) <- this line calls the static overload, I guees, instead of testing whether the id is set to an object or not. How do I test
6
3548
by: Bill foust | last post by:
I'm running into a situation there I think an operator overload would solve the issue, but I'm unable to make it work for some reason. If anyone can help here I would appreciate it. I have a base class that is common to many other classes. public class Base .... end class I have 2 seperate classes that inherit from base
6
2939
by: edd | last post by:
Hello all, Is there a way to determine whether a particular type supports the -> operator at compile time? I'm trying to write a template function (or a series of overloads) that will yield the raw pointer at "the end of the arrow". For types that support the operator, I have a reasonable solution, but I'd like to have a null pointer returned if the operator isn't supported by a particular object.
0
8969
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8788
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9335
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9263
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9208
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6053
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4570
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4825
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2193
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.