* * *

Author Topic: Question about assignment operator overloading example in the Reference guide  (Read 536 times)

440bx

  • Sr. Member
  • ****
  • Posts: 331
Hello,

Starting on page 212 about overloading the assignment operator the Reference Guide gives an example starting with the definition of a complex data type as:

Code: Pascal  [Select]
  1. type
  2.   complex = record
  3.     re : real;
  4.     im : real;
  5.   end;
  6.  
then states "This system can be used to declare a new type, and define an assignment for that type. For instance, to be able to assign a newly defined type ’Complex’ "

Code: Pascal  [Select]
  1. Var
  2.   C, Z : Complex; // New type complex
  3.  
  4. begin
  5.   Z := C;         // assignments between complex types.
  6. end;
  7.  

followed by: "The following assignment operator would have to be defined:"
Code: Pascal  [Select]
  1. Operator := (C : Complex) z : complex;
  2.  
but that makes the compiler unhappy, outputting the error message:

"Error: impossible to overload assignment for equal types"

which makes sense since the overload in that case is unnecessary.

Is the example faulty or am I missing its point ?

I also checked the ucomplex unit to see if an assignment operator was defined for a (complex, complex) tuple and there wasn't one. That example is at least confusing.

Thank you for your help.



Blaazen

  • Hero Member
  • *****
  • Posts: 2656
  • POKE 54296,15
    • Eye-Candy Controls
I can confirm with trunk. What is your FPC version?

The guide should be for 3.0.4, page is here: https://www.freepascal.org/docs-html/ref/refse101.html#x210-23200015.3

Maybe it is outdated part of guide. AFAIK only this overload is necessary now:
Code: Pascal  [Select]
  1. operator := (r : real) z : complex;
  2.      begin
  3.        z.re:=r;
  4.        z.im:=0.0;
  5.     end;
Lazarus 2.1.0 r59474M FPC 3.3.1 r40247 x86_64-linux-qt Chakra, Qt 4.8.7/5.11.2, Plasma 5.14.2
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.14

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

440bx

  • Sr. Member
  • ****
  • Posts: 331
Hi Blaazen,

the Free Pascal Reference Guide I'm reading is stated to be for version 3.0.2.  Document version 3.0.2 as well, dated February 2017.

my environment is as follows:

Free Pascal v3.0.4
Lazarus v.1.8.2
Windows 7 64bit.


In the page you linked to, the compiler will complain about the statement:

Code: Pascal  [Select]
  1. Operator := (C : Complex) z : complex;

located on the 11th line (not counting blank lines.) It makes sense for the compiler to complain since for the same type, there is no need for an assignment overload which could actually cause problems.

It looks like the web page has the same problem as the Reference Guide pdf I am reading.

Thank you for looking into it.





 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus