From: Jeroen v. d. Z. <je...@fo...> - 2008-02-26 18:32:08
|
On Tuesday 26 February 2008, Marc DUREN wrote: > Why not subclass operators in FXString ? > > FXString& operator=(int v); > FXString& operator=(double v); > > and > operator int()const; > operator double()const; > > and contructors > FXString(int v); > FXString(double v); > > so you can write this; > > FXString string; > for(i=0; i<N; i++){ > string=data[i];//FXString& operator=(int v); > // do something with string > } > > or > > int i=textfield->getText();//operator int()const; > > OR > int i=10; > textfield->setText(i); //which use the constructor FXString(int v); > > ps:I use a same technique for setting or retrieving values from database. > > Jeroen van der Zijp a écrit : For the same reason you have to write string.text() in order to get to the pointer:- avoiding unanticipated side-effects. For example: string=0; // Make it "", or set it to "0" or "0.0"?? You can probably find problems for the other overloads as well. Not that I don't like the elegance of your suggestions, mind you! But we would make some things more perspicacious if we actually had to write the conversions explicitly. Then the source code actually holds a clue as to what's going on, instead of having to carefully scrutinize all the overloads and apply C++ promotion rules. - Jeroen |