// Overload true.
publicstaticbooloperatortrue(ThreeD op) {
if((op.x != 0) || (op.y != 0) || (op.z != 0))
returntrue; // at least one coordinate is non-zero
elsereturnfalse;
}
// Overload false.
publicstaticbooloperatorfalse(ThreeD op) {
if((op.x == 0) && (op.y == 0) && (op.z == 0))
returntrue; // all coordinates are zero
elsereturnfalse;
}
publicclass TrueFalseDemo {
publicstaticvoid 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.
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.
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?