Overload False operator

Page 1 of 1

10 Replies - 2211 Views - Last Post: 03 March 2010 - 04:42 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=159390&amp;s=52163b1c977cb108913de6b4b40a123a&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 kenryuakuma

• D.I.C Regular

Reputation: 4
• Posts: 405
• Joined: 14-December 08

Overload False operator

Posted 02 March 2010 - 04:35 PM

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:

```// 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 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.

This post has been edited by kenryuakuma: 02 March 2010 - 04:36 PM

Is This A Good Question/Topic? 0

Replies To: Overload False operator

#2 MentalFloss

• .

Reputation: 576
• Posts: 1,500
• Joined: 02-September 09

Re: Overload False operator

Posted 02 March 2010 - 04:45 PM

If you overload a comparison operator, you have to overload its opposite too.

You might find this helpful: http://msdn.microsof...28VS.71%29.aspx

Also, overloading operators is a pretty delicate task so try not to go overboard for the sake of your code's readability.
Was This Post Helpful? 0

#3 kenryuakuma

• D.I.C Regular

Reputation: 4
• Posts: 405
• Joined: 14-December 08

Re: Overload False operator

Posted 02 March 2010 - 05:09 PM

MentalFloss, on 02 March 2010 - 03:45 PM, said:

If you overload a comparison operator, you have to overload its opposite too.

You might find this helpful: http://msdn.microsof...28VS.71%29.aspx

Also, overloading operators is a pretty delicate task so try not to go overboard for the sake of your code's readability.

Well...That does not really answer my question...As you can see, the true overloaded operator already does the job of returning true and false, so when will be the overloaded false operator invoked? And besides if the true overloaded operator does the job, what codes should be included in the false overloaded operator? The concept is if you overload true operator, you have to overload the opposite but one must know when the opposite operator get overloaded because true would be the first, always the first to be overloaded, so when will be the false operator overloaded? If it gets invoked what codes should be in there? It doesn't seem to make any sense to me why the false overloaded operator has to exist.
Was This Post Helpful? 0

#4 MentalFloss

• .

Reputation: 576
• Posts: 1,500
• Joined: 02-September 09

Re: Overload False operator

Posted 02 March 2010 - 05:51 PM

Well, from the link I provided, navigate here: http://msdn.microsof...28VS.71%29.aspx

Quote

Note The comparison operators, if overloaded, must be overloaded in pairs; that is, if == is overloaded, != must also be overloaded. The reverse is also true, and similar for < and >, and for <= and >=.

I suppose you can just ask the same thing about uninitialized members being used causing a problem. Why can't it just use the default value (string = string.Empty, bool = false, int = 0, etc)? Why does it need to be specified? Who cares.... it's inconsequential. The language designers decided it made sense and implemented it that way.

So, we have to play in their sandbox.
Was This Post Helpful? 0

#5 kenryuakuma

• D.I.C Regular

Reputation: 4
• Posts: 405
• Joined: 14-December 08

Re: Overload False operator

Posted 02 March 2010 - 08:40 PM

I know but when does the false overloaded operator get invoked or called? That's what baffles me.
Was This Post Helpful? 0

#6 MentalFloss

• .

Reputation: 576
• Posts: 1,500
• Joined: 02-September 09

Re: Overload False operator

Posted 03 March 2010 - 07:09 AM

Quote

```if(a) Console.WriteLine("a is true.");
else Console.WriteLine("a is false.");

```

Change if(a) to if(!a). Then put a breakpoint on false. Does it get called?
I don't have enough time to test this so I leave it to you to report the findings. I'm betting that this will call false. If this is the case, you can revise the false overload to be:

```// Overload false.
public static bool operator false(ThreeD op) {
return !(op);
}

```

I think this will work - but maybe not. Give it a shot. I'm interested in the outcome.

PS: the syntax there might need a little manipulation.
Was This Post Helpful? 0

#7 kenryuakuma

• D.I.C Regular

Reputation: 4
• Posts: 405
• Joined: 14-December 08

Re: Overload False operator

Posted 03 March 2010 - 07:50 AM

I will try that but don't think it is gonna work this way because we have another not(!) overloaded operator which is something like this

```// Overload false.
public static bool operator !(ThreeD op) {
//
}
```

if you return true, the outcome of (!a) is true, and if you return false, the outcome of (!a) is false, there is no way for you to control the (!a). When dealing with regular operator, we first know the outcome such as result = true, or result = false, then we use the not (!) operator to reverse it (!result), but overloaded (!) doesn't seem to work this way. Another confusion.
Was This Post Helpful? 0

#8 janne_panne

• WinRT Dev

Reputation: 428
• Posts: 1,047
• Joined: 09-June 09

Re: Overload False operator

Posted 03 March 2010 - 08:32 AM

Copied from MSDN:

Quote

A type cannot directly overload the conditional logical operators (&& and ||), but an equivalent effect can be achieved by overloading the regular logical operators and operators true and false (see 7.11.2 User-defined conditional logical operators)

7.11.2 User-defined conditional logical operators:
http://msdn.microsof...312(VS.71).aspx
.
Was This Post Helpful? 0

#9 kenryuakuma

• D.I.C Regular

Reputation: 4
• Posts: 405
• Joined: 14-December 08

Re: Overload False operator

Posted 03 March 2010 - 04:09 PM

When it comes to true and false overloaded operator, MSDN does not seem to help a bit...Maybe it is too confusing for noobs. Hope there would be an accurate and satisfactory answer to this.
Was This Post Helpful? 0

#10 MentalFloss

• .

Reputation: 576
• Posts: 1,500
• Joined: 02-September 09

Re: Overload False operator

Posted 03 March 2010 - 04:11 PM

Don't worry about it kenryuakuma. Operator overloading is generally a stupid idea anyway.
Was This Post Helpful? 0

#11 janne_panne

• WinRT Dev

Reputation: 428
• Posts: 1,047
• Joined: 09-June 09

Re: Overload False operator

Posted 03 March 2010 - 04:42 PM

Quote

Operator overloading is generally a stupid idea anyway.

That could be a good topic idea for C# programmers sub forum: do people overload operators in C# and why/why not. If someone just has enough motivation to create the topic.. afk ->
Was This Post Helpful? 0

Page 1 of 1

 .related ul{list-style-type:circle;font-size:12px;font-weight:bold;}.related li{margin-bottom:5px;background-position:left 7px!important;margin-left:-35px;}.related h2{font-size:18px;font-weight:bold;}.related a{color:blue;}