DGL
https://delphigl.com/forum/

C++ Class Operator in Pascal
https://delphigl.com/forum/viewtopic.php?f=19&t=4798
Seite 1 von 2

Autor:  Gast [ Mi Nov 02, 2005 12:24 ]
Betreff des Beitrags:  C++ Class Operator in Pascal

Hello guys,

Since you are working on Quake 3 to Delphi conversion, maybe you have some ideas on the following problem:

In C++ there is a function to assign operator to a class,i thougt it was called Operator Overloading.
Let's say we got a vector class:

Code:
  1.  
  2. vector {
  3.    float   x,y,z  
  4. }
  5.  


In C++ it is possible to make a function which handles, for example, the substration of the 2 classes.
So when you use

Code:
  1.  
  2. vector1 - vector2
  3.  


it is using your own made function.

I know that with Delphi 2005 and .NET it is possible, but how do you guys solve it without .NET?

Thanks in advance

-Ice3

Autor:  sniper [ Mi Nov 02, 2005 12:55 ]
Betreff des Beitrags: 

In delphi32 there is no operator overloading. You simply must write your own function witch does that. And if you declare and implement the function in the same unit where you declared your classes, this function could serve as an "friend" function in C++ manner.

Example:
Code:
  1. type
  2.   TVector = class
  3.     private
  4.       fX, fY, Fz : Single;
  5.     public
  6.       property X:Single read fX write fX;
  7.       property Y:Single read fY write fY;
  8.       property Z:Single read fZ write fZ;
  9.   end;
  10.  
  11.  
  12. procedure VectorAdd( v1, v2, result :TVector );
  13.  
  14. implementation
  15.  
  16. procedure VectorAdd( v1, v2, result :TVector );
  17. begin
  18.   result.fX := v1.fX + v2.fX; // you cann access the private members since you are in the same unit !!!
  19.   result.fY := v1.fY + v2.fY; // this is like a friend function in c++
  20.   result.fZ := v1.fZ + v2.fZ;
  21. end;
  22.  


And Quake3 is written in C, so there are no classes there, and of course no operator overloading.

Autor:  LarsMiddendorf [ Mi Nov 02, 2005 13:33 ]
Betreff des Beitrags: 

Delphi2006 supports operator overloading also in Win32. The syntax will be the same as for the .Net version.

Autor:  Gast [ Fr Nov 04, 2005 10:44 ]
Betreff des Beitrags: 

Thanks, you both!

I will use sniper_w code for now, but when Delphi 2006 comes out, i'll look into that!

Thanks again

-Ice3

Autor:  TAK2004 [ Do Dez 01, 2005 09:29 ]
Betreff des Beitrags: 

You can use FreePascal, it support stuff like this.
You find it under this link http://www.freepascal.org

irony_mode:=true;
But it is a free compiler and you don't pay money, its cross and build much faster code like delphi and gcc.
It have no ide and for OpenGL you need a visuell development envoirement because only on this way you can use the not existing vcl on your OGL Projekt.
irony_mode:=false;

Autor:  delphiXYZ [ Do Dez 08, 2005 13:15 ]
Betreff des Beitrags: 

sniper hat geschrieben:
In delphi32 there is no operator overloading. You simply must write your own function witch does that. And if you declare and implement the function in the same unit where you declared your classes, this function could serve as an "friend" function in C++ manner.

Example:
Code:
  1. type
  2.   ............
  3.  


And Quake3 is written in C, so there are no classes there, and of course no operator overloading.


and C#? Sorry C are more performant language, as english compare with german's + ae, oe, ue etc.

MfG
berlinea screen TFT

Autor:  sniper [ Do Dez 08, 2005 18:14 ]
Betreff des Beitrags: 

Zitat:
and C#? Sorry C are more performant language, as english compare with german's + ae, oe, ue etc.
:shock: If only i could understand what you mean with this ad what it has to do with anything i said.

Autor:  LarsMiddendorf [ Do Dez 08, 2005 19:30 ]
Betreff des Beitrags: 

He probably means that a pure language without any extensions is the fastest, but that's obviously wrong.

Autor:  TAK2004 [ Do Dez 08, 2005 20:46 ]
Betreff des Beitrags: 

The fastest language is that which use a compiler who generate the fastest asm routine(not the shortest because every asm call need different cycles for a call).
Thats mean if you have a compiler which check every assignment for valid values and have no checker for stupid codeblocks like "if 1=1 then" or use other not needed code it will be slow down the binary. C must not the fastest and Java not the slowest because its dependent on compiler.

Autor:  Grizzly [ Fr Dez 09, 2005 23:01 ]
Betreff des Beitrags: 

not to forget the degree of optimizations a specific compiler depends mostly on the kind of routines it's given, because even the compiler generating the fastest code in general has some points, where it doesn't optimize to the perfect implementation (probably most of the time) and other compilers which are optimized better for this job will generate a better solution for the given task

Autor:  Flash [ So Dez 11, 2005 03:08 ]
Betreff des Beitrags: 

Java code is defenitly slower then normal c/pascal code because it has to be "compiled" at the first run. But that's not the point... ;)

Autor:  delphiXYZ [ Do Dez 15, 2005 00:14 ]
Betreff des Beitrags:  MagicGeometry

Code:
  1.  
  2. type
  3.       TMagicVector = class
  4.       private
  5.       FX, FY, FZ, FU : Single;
  6.       public
  7.  
  8.       property X:Single read FX write FX;
  9.       property Y:Single read FY write FY;
  10.       property Z:Single read FZ write FZ;
  11.       property Z:Single read FU write FU;
  12.  
  13.       end;
  14.  
  15.       procedure VectorAdd( v1, v2, result :TMagicVector );
  16.  
  17.       implementation
  18.  
  19.       procedure VectorAdd( v1, v2, result :TMagicVector );
  20. .
  21.       begin
  22.  
  23.       result.FX := v1.FX + v2.FX;
  24.       result.FY := v1.FY + v2.FY;
  25.       result.FZ := v1.FZ + v2.FZ;
  26.       result.FU := v1.FU + v2.FU;
  27.  
  28.       end;
  29.  

Autor:  delphiXYZ [ Do Dez 15, 2005 00:16 ]
Betreff des Beitrags: 

Flash hat geschrieben:
Java code is defenitly slower then normal c/pascal code because it has to be "compiled" at the first run. But that's not the point... ;)
and Quick Basic is "implementator", other ? 8)

Autor:  delphiXYZ [ Do Dez 15, 2005 00:22 ]
Betreff des Beitrags: 

sniper hat geschrieben:
.......
Code:
  1.  
  2.   TVector = class
  3.     private
  4.       fX, fY, Fz : Single; //sorry C will upper letter error message debugged Fz or fZ?
  5.     public
  6.       property X:Single read fX write fX;
  7.       property Y:Single read fY write fY;
  8.       property Z:Single read fZ write fZ;
  9.   end;
  10.  
  11.  
  12. procedure VectorAdd( v1, v2, result :TVector );
  13.  
  14. implementation
  15.  
  16. procedure VectorAdd( v1, v2, result :TVector );
  17. begin
  18.   result.fX := v1.fX + v2.fX; // you cann access the private members since you are in the same unit !!!
  19.   result.fY := v1.fY + v2.fY; // this is like a friend function in c++
  20.   result.fZ := v1.fZ + v2.fZ;
  21. end;
  22.  


And Quake3 is written in C, so there are no classes there, and of course no operator overloading.

Autor:  Pellaeon [ Fr Dez 23, 2005 09:06 ]
Betreff des Beitrags: 

sniper hat geschrieben:
Code:
  1.  
  2.   result.fY := v1.fY + v2.fY; // this is like a friend function in c++
  3.  



That´s wrong. A friend function means, that an other class or function has access to protected and private elements of a class.

In this case it' s an instance of the same class type, so I can use all private things, because "i am in the same class type". That`s the normal way of object orientaded programming :-)

Seite 1 von 2 Alle Zeiten sind UTC + 1 Stunde
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/