Forum > General

Operator overloading: Someone please explain this

(1/3) > >>

Zvoni:
Hi Folks,

playing around i overloaded some operators

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---//Somewhere in a Unitinterface//String and LongIntOperator +(AString:String;ALongInt:LongInt):String;Overload;Operator +(ALongInt:LongInt;AString:String):String;Overload;                   ImplementationOperator +(AString:String;ALongInt:LongInt):String;Begin  Result:=AString+ALongInt.ToString;end;  Operator +(ALongInt:LongInt;AString:String):String;Begin  Result:=ALongInt.ToString+AString;end;        
Using above i stumbled across this:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---Uses MyOperatorUnit;VarMyText:String;MyNumber:Integer;        BeginMyText:='Test ';MyNumber:=123456;       Writeln(MyText+MyNumber); //WorksWriteln(MyNumber+MyText); //WorksWriteln(MyNumber+' Test'); //WorksWriteln(123456+MyText);  //WorksWriteln(MyText+123456);   //WorksWriteln('Test '+MyNumber); //Doesn't work?!?!??! --> project1.lpr(19,19) Error: Incompatible types: got "LongInt" expected "AnsiString"  
Huh? I'm looking at it, and it doesn't make any sense to me.

ASerge:

--- Quote from: Zvoni on March 18, 2019, 09:07:57 pm ---
--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---Writeln('Test '+MyNumber); //Doesn't work?!?!??! --> project1.lpr(19,19) Error: Incompatible types: got "LongInt" expected "AnsiString"Huh? I'm looking at it, and it doesn't make any sense to me.

--- End quote ---
A quoted string can be string, UnicodeString, ShortString, PChar, PWideChar and so on. Cast it: string('Test ') +...

Thaddy:
The real explanation is adding {$H+}

ASerge:

--- Quote from: Thaddy on March 18, 2019, 09:35:35 pm ---The real explanation is adding {$H+}

--- End quote ---
Check before you advise. The error occurs in {$H+}

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---{$MODE OBJFPC}{$APPTYPE CONSOLE}{$LONGSTRINGS ON} uses SysUtils; operator + (const Left: string; Right: LongInt): string;begin  Result := Left + IntToStr(Right);end; operator + (Left: LongInt; const Right: string): string;begin  Result := IntToStr(Left) + Right;end; var  Text: string = 'text';  Number: LongInt = 123456;begin  Writeln(Text + Number);  Writeln(Number + Text);  Writeln(Number + 'other text');  Writeln(789012 + Text);  Writeln(Text + 789012);  Writeln('else text' + Number); // project1.lpr(26,25) Error: Incompatible types: got "LongInt" expected "AnsiString"  Readln;end.

Zvoni:
So what's the Explanation/Solution?
You lost me along the way there....

Navigation

[0] Message Index

[#] Next page

Go to full version