overloading true and false operator

Nov 24, 2009 at 8:15pm
I am just wonder what the point is of overloading the false operator as overloading the true operator already does the job.

Let's say you have this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// Overload true.   
  public static bool operator true(ThreeD op) { 
    if((op.x != 0) || (op.y != 0) || (op.z != 0)) 
      return true; // at least one coordinate is non-zero 
    else 
      return false; 
  }   
 
  // Overload false. 
  public static bool operator false(ThreeD op) { 
    if((op.x == 0) && (op.y == 0) && (op.z == 0)) 
      return true; // all coordinates are zero 
    else 
      return false; 
  }   

public class TrueFalseDemo {   
  public static void Main() {   
    ThreeD a = new ThreeD(5, 6, 7);   
    ThreeD b = new ThreeD(10, 10, 10);   
    ThreeD c = new ThreeD(0, 0, 0);   
   
    Console.Write("Here is a: ");   
    a.show();   
    Console.Write("Here is b: ");   
    b.show();   
    Console.Write("Here is c: ");   
    c.show();   
    Console.WriteLine();   
   
    if(a) Console.WriteLine("a is true."); 
    else Console.WriteLine("a is false."); 
 
    if(b) Console.WriteLine("b is true."); 
    else Console.WriteLine("b is false."); 
 
    if(c) Console.WriteLine("c is true."); 
    else Console.WriteLine("c is false.");
 


I know these are not the C++ codes, but I know some of you do know both or even more including java. But I am just stuck with the false operator since it is not even used in the program if assuming a, the object, is always true. Besides, that how do we know what codes to write in the overload false operator method as the overload true operator already does the job true and false. If it is true returns true if false, returns false.
Nov 24, 2009 at 8:19pm
Yeah, I'm not sure why they did that...if you *have* to overload the true operator (not sure how that works in Java), you could just overload it as !false.
Nov 24, 2009 at 9:41pm
Well...This is C# code.

So as you knows, in any C based language, when you overload the true and false operators, you have to overload both of them. But the question is what invokes the false overload operator method? When the program reaches the if(a) Console.WriteLine("a is true."); This will invokes the true overload operator method and within the operator method, we already have the result of true and false. If the expression within the true overload operator method is true, then returns true, and if it is false, returns false.

Because within the if statement in main, the a, which is the object, is always assumed to be true. There is no way to invoke the false overload operator method. Just why we need it and what codes to write within the method?
Nov 25, 2009 at 12:19am
It could possibly be used when you do this:

if (!a)
Nov 25, 2009 at 12:45am
To me, that looks like it would call the true method, then the ! operator...just a guess though.
Nov 25, 2009 at 12:51am
True and false are not always disjoint. Hence, you must overload both.
Nov 25, 2009 at 1:06am
In C# there is also another overload ! operator. That even confuses me more.

it looks something like [inline]public static bool operator !(ThreeD op)[/inline]

and within the body there is something like if(op) return false; else return true;

and then the call is if(!a) Console.Write("This is false.);

Wonder how this works as well. I have been doing a bunch of research, but the result is none.
Topic archived. No new replies allowed.