Модератор: Модераторы
unit Unit1; 
{$mode objfpc}{$H+}
interface
uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls;
type
  { TForm1 }
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { private declarations }
  public
    { public declarations }
  end; 
  { TInt }
  TInt = class(TObject)
    private
       FInt:Integer;
    public
       constructor Create(value:Integer);
       destructor Destroy; override;
       property Int:Integer  read FInt write FInt;
  end;
  operator + (z1 : TInt;r : TInt) b : Integer;
var
  Form1: TForm1; 
implementation
operator+(z1: TInt; r: TInt)b: Integer;
begin
  b := z1.FInt + r.FInt;
end;
constructor TInt.Create(value:Integer);
begin
  inherited Create();
  FInt :=value;
end;
destructor TInt.Destroy;
begin
  inherited Destroy;
end;
{ TInt }
{$R *.lfm}
{ TForm1 }
procedure TForm1.Button1Click(Sender: TObject);
var v1, v2:TInt;
begin
    v1 :=TInt.Create(12);
    v2 :=TInt.Create(10);
    Caption:=IntToStr(v1+v2);
    v1.Free;
    v2.Free;
end;
end.
{$ifdef ComplexIsSingle}
    const
      MinComplex  =  1.5e-45;
      MaxComplex  =  3.4e+38;
    Type
      PComplex =^TComplex;
      TComplex = record
         Re,
         Im:Single;
    end;// TComplex = record
{Переопределение сложения Add +}
  operator + (L : Extended; R : TComplex) z : TComplex;
  operator + (L : TComplex; R : Extended) z : TComplex;
  operator + (L : TComplex; R : TComplex) z : TComplex;
 {Переопределение сложения Add +}
operator + (L : Extended; R : TComplex) z : TComplex;
begin
  z.Re:=R.Re+L;
  z.Im:=R.Im;
end;
 //------------------------------------------------------------------//
operator + (L : TComplex; R : Extended) z : TComplex;
begin
  z.Re:=L.Re+R;
  z.Im:=L.Im;
end;
//------------------------------------------------------------------//
operator + (L : TComplex; R : TComplex) z : TComplex; assembler;
{$ASMMODE intel}
asm
  FLD   TComplex.Re [EAX]
  FLD   TComplex.Re [EDX]
  FADD
  FSTP  TComplex.Re [ECX]
  FLD   TComplex.Im [EAX]
  FLD   TComplex.Im [EDX]
  FADD
  FSTP  TComplex.Im [ECX]
//begin
  //    z.Re:=L.Re+R.Re;
  //    z.Im:=L.Im+R.Im;
  end;
//------------------------------------------------------------------//
 TInt = class(TObject)
    private
       FInt:Integer;
    public
       data :integer; 
       constructor Create(value:Integer);
       destructor Destroy; override;
       property Int:Integer  read FInt write FInt;
      operator + (z1 : TInt;r : TInt) b : Integer;
  end; operator TInt. +(z1: TInt; r: TInt)b: Integer;
begin
data:=data+1;
  b := z1.FInt + r.FInt;
end;
TInt = class(TObject)
    private
       FInt:Integer;
    public
       data :integer; 
       constructor Create(value:Integer);
       destructor Destroy; override;
       property Int:Integer  read FInt write FInt;
      operator + (z1 : TInt;r : TInt) b : Integer;
       begin
       data:=data+1;
        b := z1.FInt + r.FInt;
       end;
  end;
class operator Add (z1 : TInt;r : TInt) : Integer;
class operator +(z1 : TInt;r : TInt) : Integer;
Maxizar писал(а): вы хотели что то прибавить, и увеличить что-то внутри, или прибавить переменную A к B, а увеличить счетчик в C.
Это в любом случае не сработает - оператор = статический (классовый) метод.
Mike81 писал(а):Спасибо Большое за развернутый ответ и ссылку на guide for Free Pascal. Я, правда, имел ввиду немного другое.
Мне это нужно затем, чтобы при описании оператора + я мог пользоавться внутренними переменными класса, например переменной data которую я ввел искуственно в этом примере.
но похоже, такой возможности нет. Еще раз спасибо за ответ...
Mike81 писал(а):Спасибо Большое за развернутый ответ и ссылку на guide for Free Pascal. Я, правда, имел ввиду немного другое.
ну то есть что-то навроде такого
- Код: Выделить всё
 operator TInt. +(z1: TInt; r: TInt)b: Integer;
begin
data:=data+1;
b := z1.FInt + r.FInt;
end;
operator +(z1: TInt; r: TInt)b: Integer;
begin
  z1.data:=z1.data+1;
  b := z1.FInt + r.FInt;
end;
Вернуться в Free Pascal Compiler
Сейчас этот форум просматривают: Yandex [Bot] и гости: 1