# Object value displayed and false overload operator

Page 1 of 1

## 4 Replies - 818 Views - Last Post: 26 November 2009 - 09:22 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=141713&amp;s=73e26583bea2b5a45c18efd8a1d9dc26&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: 408
• Joined: 14-December 08

# Object value displayed and false overload operator

Posted 26 November 2009 - 11:01 AM

```using System;

// A three-dimensional coordinate class.
class ThreeD {
int x, y, z; // 3-D coordinates

public ThreeD() { x = y = z = 0; }
public ThreeD(int i, int j, int k) { x = i; y = j; z = k; }

public static ThreeD operator +(ThreeD op1, ThreeD op2)
{
ThreeD result = new ThreeD();

/* This adds together the coordinates of the two points
and returns the result. */
result.x = op1.x + op2.x; // These are integer additions
result.y = op1.y + op2.y; // and the + retains its original
result.z = op1.z + op2.z; // meaning relative to them.

return result;
}

public static ThreeD operator -(ThreeD op1, ThreeD op2)
{
ThreeD result = new ThreeD();

/* Notice the order of the operands. op1 is the left
operand and op2 is the right. */
result.x = op1.x - op2.x; // these are integer subtractions
result.y = op1.y - op2.y;
result.z = op1.z - op2.z;

public static implicit operator int(ThreeD op1)
{
return op1.x * op1.y * op1.z;
}

return result;
}

// Show X, Y, Z coordinates.
public void show()
{
Console.WriteLine(x + ", " + y + ", " + z);
}
}

public class ThreeDDemo {
public static void Main() {
ThreeD a = new ThreeD(1, 2, 3);
ThreeD b = new ThreeD(10, 10, 10);
ThreeD c = new ThreeD();

Console.WriteLine("Here is a: {0}", (int) a);
Console.WriteLine("Here is b: {0}", (int) b);

c = a + b; // add a and b together
Console.Write("Result of a + b: {0}", (int) c);

Console.WriteLine();

c = a + b + c; // add a, b and c together
Console.Write("Result of a + b + c: ");
c.show();
Console.WriteLine();

c = c - a; // subtract a
Console.Write("Result of c - a: ");
c.show();
Console.WriteLine();

c = c - b; // subtract b
Console.Write("Result of c - b: ");
c.show();
Console.WriteLine();
}
}
```

```// 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;
}

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.");
```

Anyway, the first batch of codes is all about how the values object are displayed and the second batch of codes is about the false overload operator.

Well...Usually when we want to display the value of the object, we usually do this and invoke our constructor by doing this. First, creating an object and along with some values: ThreeD a = new ThreeD(1, 2, 3);, and then the constructor being invoked, values being assigned to the instance variables. In this case, public ThreeD(int i, int j, int k) { x = i; y = j; z = k; }.

But the question is we usually invoke the method of the object class to display the object values and if we want to cast the values of the object, we have to declare another overload operator method, which is something like

```public static explicit operator int(ThreeD op1)
{
return op1.x * op1.y * op1.z;
}
```

for implicit overload operator we do something like this

```public static implicit operator int(ThreeD op1)
{
return op1.x * op1.y * op1.z;
}
```

1) But as you can see the Console.WriteLine("Here is a: {0}", (int) a);, it is displaying the object values. But if you take a closer look, this one will invoke the explicit overload operator, but we only have the implicit overload operator and we don't have the explicit overload operator. So how can the object values be displayed in this way? and I just don't understand.

```public static bool operator true(ThreeD op) {
Console.WriteLine("Yoohoo I am in trueee");
if((op.x != 0) || (op.y != 0) || (op.z != 0))
return true; // at least one coordinate is non-zero
else
return false;
}

public static bool operator false(ThreeD op) {
Console.WriteLine("Yoohoo I am in faaallseee");
if((op.x == 0) && (op.y == 0) && (op.z == 0))
return true; // all coordinates are zero
else
return false;
}
```

This post has been edited by kenryuakuma: 26 November 2009 - 11:04 AM

Is This A Good Question/Topic? 0

## Replies To: Object value displayed and false overload operator

### #2 Momerath

• D.I.C Lover

Reputation: 1020
• Posts: 2,463
• Joined: 04-October 09

## Re: Object value displayed and false overload operator

Posted 26 November 2009 - 12:20 PM

1) Implicit operator will be called if you don't have an explicit one. And Implicit operator should never throw an exception and never lose information. An explicit operator can do both (and you see it when you try to convert double to int implicitly). It is dependant on you, the coder, to make sure that implicit operators always work.

2) The complier optimizes your code, and sometimes it will reverse the check from if (a) to if (!a) internally if it determines that the code would be faster, smaller, or whatever you have optimization set at.

This holds true for all the pair operators (like == and !=, < and >=, etc.). This is why you must overload them in pairs.

### #3 kenryuakuma

• D.I.C Regular

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

## Re: Object value displayed and false overload operator

Posted 26 November 2009 - 03:59 PM

First of all, HAPPY THANKSGIVING Momerath! Turkey time!

Anyway, will the program generate error messages if there is no overload operator method of implicit or explicit if we do something like this Console.WriteLine("Here is a: {0}", (int) a);?

Besides, since the false overload operator is hardly called, I am just wondering what statements should be included within the body of the false overload operator method?

### #4 SixOfEleven

• Planeswalker

Reputation: 1055
• Posts: 6,643
• Joined: 18-October 08

## Re: Object value displayed and false overload operator

Posted 26 November 2009 - 06:37 PM

kenryuakuma, on 26 Nov, 2009 - 04:59 PM, said:

First of all, HAPPY THANKSGIVING Momerath! Turkey time!

Anyway, will the program generate error messages if there is no overload operator method of implicit or explicit if we do something like this Console.WriteLine("Here is a: {0}", (int) a);?

Besides, since the false overload operator is hardly called, I am just wondering what statements should be included within the body of the false overload operator method?

If you try and cast it like that and there is no overloaded operator to do that you would more than likely get a compile time error. If you didn't you would surely get a run time exception.

### #5 kenryuakuma

• D.I.C Regular

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

## Re: Object value displayed and false overload operator

Posted 26 November 2009 - 09:22 PM

Cool...Now it is all clear up. Again, I owe you a thank SixOfEleven.

BTW...Happy Thanksgiving! I have also read your gaming tutorial, although not understand what they are really about...But the pictures speak the words. Really fantastic work.