www.digitalmars.com         C & C++   DMDScript  

D - Operator overloading (should it work like this?)

a few points, one why is add, sub etc not effectively
template( class T ) { class T { T add( T, T ); T add( int );  } ?
so a = c+b; will call a.add( c, b ); a++, ++a will call t.add( (int)1 );
a+= b calls a.add( a, b );  a+=3 calls a.add( (int)3 );

also `++obj` doesnot work gives the error 'obj' is not a scalar, it is a
MyObject

also I do not see why if ++obj will be the same as obj+=1, why obj++ is not
also
like so;
myFunc( ++obj ) -> obj+=1; myFunc( obj );
myFunc( obj )    -> myFunc( obj );obj+=1;

also i = 0; myFunc( i++ ); // myFunc is called with 0, i =1 after the call
but NOT for obj++ which is evaled before the call
see this code;

class Test
{
public:
 int val=0;

 int postinc() { printf("postinc %d",val); val++; printf(" + 1 = %d\n",
val ); return val; }
 int add( int i ) { printf("add %d", val); val += i; printf(" + %d = %d\n",
i, val ); return val; }
 void println() { printf("val = %d\n", val); }
}

void show( Test t ) { printf( "show Test -> " );t.println(); }
void show( int i )  { printf( "show int : %d\n", i ); }

int main( char[][] args )
{
 Test t = new Test;
 printf( "\nshow( t ); ->\n" );
 show( t );
 printf( "\nt+1;show( t ); ->\n" );
 t + 1;
 show( t );
// does not work
// printf( "\nshow( ++t ); ->\n" );
// show( ++t );
 printf( "\nshow( t++ ); ->\n" );
 show( t++ );
 printf( "\nshow( t ); ->\n" );
 show( t );
 int i = 0;
 printf( "\n\nint i =0; show( i )->\n" );
 show( i );
 printf( "\nshow( ++i )->\n" );
 show( ++i );
 printf( "\nshow( ++i )->\n" );
 show( i++ );
 printf( "\nshow( i )->\n" );
 show( i );
 return 0;
}
 outputs the following.

show( t ); ->
show Test -> val = 0

t+1;show( t ); ->
add 0 + 1 = 1
show Test -> val = 1

show( t++ ); ->
postinc 1 + 1 = 2
show int : 2

show( t ); ->
show Test -> val = 2


int i =0; show( i )->
show int : 0

show( ++i )->
show int : 1

show( ++i )->
show int : 1

show( i )->
show int : 2
Oct 04 2002