Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i override a base class's == operator, so the override gets called

With code like the following

public class Task
{
  string Name;
  public static bool operator ==(Task t1, Task t2)
  { return t1.Name = t2.Name && t1.GetType() == t2.GetType(); }
}
public class TaskA : Task
{
   int aThing;
   public static bool operator ==(TaskA t1, TaskA t2)
   { 
      return (Task)t1 == (Task)t2 && t1.GetType() == t2.GetType()
          && t1.aThing == t2.aThing; }
}
public class TaskB : Task  //more of the same

class Stuffin
{
   List<Task> Tasks;

   void CheckIt()
   {
      bool theSame = Tasks[0] == Tasks[1];
   }

I'm trying to make sure that the derived operator (TaskA.==) is called.

I get compilation error when trying the technique here.

I think i'd be able to get it to work correctly if the operator was not static, because i could then override the base class's operator. Is that possible?

Once i get that how would i compare the base properties (i would think the cast to task type [(Task)t1 == (Task)t2] would not work)?

like image 654
rediVider Avatar asked Jun 25 '10 21:06

rediVider


People also ask

Can you override == operator in Java?

Java doesn't supports operator overloading because it's just a choice made by its creators who wanted to keep the language more simple. Every operator has a good meaning with its arithmetic operation it performs.

Can you override operator?

Operator overloading cannot change the precedence and associativity of operators. However, if we want to change the order of evaluation, parentheses should be used. There are 4 operators that cannot be overloaded in C++.

Can we override operator in C++?

You can redefine or overload the function of most built-in operators in C++. These operators can be overloaded globally or on a class-by-class basis. Overloaded operators are implemented as functions and can be member functions or global functions.

How to properly override a base class method?

How to “properly” override a base class method? 1 Call base.Method (), and then provide my implementation. 2 Provide my implementation and then call base.Method () 3 Just provide my implementation. More ...

How to override a method in a derived class in Java?

1 Method overriding is possible only in derived classes. Because a method is overridden in the derived class from the base class. 2 A non-virtual or a static method can’t be overridden. 3 Both the override method and the virtual method must have the same access level modifier.

How to call parent class from sub class in Override method?

Invoking overridden method from sub-class : We can call parent class method in overriding method using super keyword . Overriding and constructor : We can not override constructor as parent and child class can never have constructor with same name (Constructor name must always be same as Class name).

What is method overriding in C++?

Method Overriding in C# is similar to the virtual function in C++. Method Overriding is a technique that allows the invoking of functions from another class (base class) in the derived class. Creating a method in the derived class with the same signature as a method in the base class is called as method overriding.


1 Answers

You can't. Operators aren't overridden, they're overloaded. That means the implementation to be used is entirely decided at compile-time.

One thing you can do is override Equals in Task and call it from ==, and then override it again in TaskA. That also makes the "base properties" check easy - just call base.Equals from TaskA.Equals.

like image 167
Jon Skeet Avatar answered Nov 14 '22 23:11

Jon Skeet