www.digitalmars.com         C & C++   DMDScript  

D - operator overloading

reply "Ivan Senji" <ivan.senji public.srce.hr> writes:
1. i like D very much1
2. I only recently got some time to start exploring it and i have a
question:
i have this code:

class Cout
{
 Cout opShl(char[] x)
 {
  printf("%.*s",x);
  return this;
 }
 Cout opShl(int x)
 {
  printf("%d",x);
  return this;
 }
    ...
}

The idea is to make a class cout that would act like c++ cout.
Is there any way to write something like:
Cout opShl(Cout c,A f)
{
       //printf that prints object f of type A
  return c;
}

it doesn't work the way i would like it, i have to explicitly call the
function ti print type A!
Jan 25 2004
next sibling parent "Robert" <no spam.ne.jp> writes:
opShl_r will make you happy!


"Ivan Senji" <ivan.senji public.srce.hr> wrote in message
news:bv163k$ckg$1 digitaldaemon.com...
 1. i like D very much1
 2. I only recently got some time to start exploring it and i have a
 question:
 i have this code:

 class Cout
 {
  Cout opShl(char[] x)
  {
   printf("%.*s",x);
   return this;
  }
  Cout opShl(int x)
  {
   printf("%d",x);
   return this;
  }
     ...
 }

 The idea is to make a class cout that would act like c++ cout.
 Is there any way to write something like:
 Cout opShl(Cout c,A f)
 {
        //printf that prints object f of type A
   return c;
 }

 it doesn't work the way i would like it, i have to explicitly call the
 function ti print type A!
Jan 25 2004
prev sibling parent "Phill" <phill pacific.net.au> writes:
what if you have a toString() function in A
and then call
printf( f.toString());

Phill.


"Ivan Senji" <ivan.senji public.srce.hr> wrote in message
news:bv163k$ckg$1 digitaldaemon.com...
 1. i like D very much1
 2. I only recently got some time to start exploring it and i have a
 question:
 i have this code:

 class Cout
 {
  Cout opShl(char[] x)
  {
   printf("%.*s",x);
   return this;
  }
  Cout opShl(int x)
  {
   printf("%d",x);
   return this;
  }
     ...
 }

 The idea is to make a class cout that would act like c++ cout.
 Is there any way to write something like:
 Cout opShl(Cout c,A f)
 {
        //printf that prints object f of type A
   return c;
 }

 it doesn't work the way i would like it, i have to explicitly call the
 function ti print type A!
Jan 25 2004