roxen.lists.pike.general

Subject Author Date
overloading again. Mirar @ Pike importmöte för m <6341[at]lyskom[dot]lysator[dot]liu[dot]se> 29-01-2009
Here, have an example:

   class TheX
   {
      int x;
      string the;
      
      void create(int _x,string _the)
      {
         x=_x;
         the=_the;
      }
   
      TheX `+(object|int y)
      {
         if (intp(y))
            return TheX(x+y,the+"+"+y);
         else if (objectp(y) && y->x && y->the)
            return TheX(x+y->x,the+"+"+y->the);
         else
            error("can't add %O and %O\n",this,y);
      }
   
      TheX `+=(object|int y)
      {
         if (intp(y))
            x+=y,the+="+"+y;
         else if (objectp(y) && y->x && y->the)
            x+=y->x,the+="+"+y->the;
         else
            error("can't add %O and %O\n",this,y);
   
         return this;
      }
   
      string _sprintf(int t)
      {
         if (t=='O') return sprintf("TheX(%O,%O)",x,the);
         return 0;
      }
   }
   
   int main()
   {
      TheX a=TheX(17,"seventeen");
      TheX b=TheX(42,"fortytwo");
   
      werror("%O\n",a+b);
      werror("%O\n",a+4711);
   
      a+=b;
      werror("%O\n",a);
   
      b+=4711;
      werror("%O\n",b);
   
      return 0;
   }

Should output:

  TheX(59,"seventeen+fortytwo")
  TheX(4728,"seventeen+4711")
  TheX(59,"seventeen+fortytwo")
  TheX(4753,"fortytwo+4711")

/Mirar