Robert Ryan wrote:
> this is what I am working on
>   #include <iostream>
>    
>   int add (int x, int y) {
>       int z;          
>       z = x + y;
>       return (z);     
>   }
>    
>   int sub (int x, int y) {
>       int z;          
>       z = x - y;
>       return (z);     
>   }
>    
>   int multi (int x, int y) {
>       int z;          
>       z = x * y;
>       return (z);     
>   }
>    
>   int div (int x, int y) {
>       int z;          
>       z = x / y;
>       return (z);     
>   }
>    
>   main ()
>   {
>   int i, j, k1, k2, k3, k4;
>   i = 10;
>   j = 20;
>   k1 =  + (i, j);    
>   k2 =  -  (i, j);
>   k3 =  *  (i, j);
>   k4 =  /   (i, j);          
>        cout("The result of k1, k2, k3 ,k4 is:   \n", k1, k2, k3, k4);  
>   }

As I've already stated:  You can't do that.  Overloading primitive 
operators is explicitly disallowed by the ANSI C++ Standard.

 From 13.5.6 of my Draft copy of the ANSI C++ Standard:
---8<--------------------------------------------------
An operator function shall either be a non-static member function or be 
a non-member function and have at least one parameter whose type is a 
class, a reference to a class, an enumeration, or a reference to an 
enumeration. It is not possible to change the precedence, grouping, or 
number of operands of operators. The meaning of the operators =, (unary) 
&, and , (comma), predefined for each type, can be changed for specific 
class and enumeration types by defining operator functions that 
implement these operators. Operator functions are inherited in the same 
manner as other base class functions.
---8<--------------------------------------------------

So there you have it.  You can't create an overloaded operator for 
primitive types.  The Standard explicitly doesn't allow it and therefore 
neither will (nor should) your compiler.

Therefore, you can't do what you are trying to do.  Attempting to do it 
is pointless.  Even if you succeed, it likely won't work on any other 
compiler or any future version of your existing compiler.

BTW, you can get your own Draft copy of the ANSI C++ Standard for free 
from the c-prog website (Draft copies are free, the real thing costs 
money, but Drafts are usually good enough).  Highly recommended!

-- 
Thomas Hruska
CubicleSoft President
Ph: 517-803-4197

*NEW* MyTaskFocus 1.1
Get on task.  Stay on task.

http://www.CubicleSoft.com/MyTaskFocus/

Reply via email to