Cross-reference between delphi records

限于喜欢 提交于 2019-12-10 18:55:00

问题


Let's say I have a record TQuaternion and a record TVector. Quaternions have some methods with TVector parameters. On the other hand, TVector supports some operations that have TQuaternion parameters.

Knowing that Delphi (Win32) does not allow for forward record declarations, how do I solve this elegantly?

Using classes is not really an option here, because I really want to use operator overloading for this rare case where it actually makes good sense.

For now I simply moved these particular methods out of the records and into separate functions, the good old-fashioned way. Better suggestions are most welcome.


回答1:


If the operators are not the actual problem you can solve this using a record helper.

type
  TVector = record
  end;

  TQuaternion = record
    procedure UseVector(var V: TVector);
  end;

  TVectorHelper = record helper for TVector
    procedure UseQuaternion(var Q: TQuaternion);
  end;

This will solve only the circular dependencies and it does not work with operators. It also has the drawback that you cannot have any other record helper for TVector, at least not both of them can be available in the same place.




回答2:


There is no elegant solution in general, only old hacks with untyped references like this:

type
  TVector = record
    procedure UseQuaternion(var Q);
  end;

  TQuaternion = record
    procedure UseVector(var V: TVector);
  end;

{ TVector }

procedure TVector.UseQuaternion(var Q);
var
  Quaternion: TQuaternion absolute Q;

begin
  Quaternion.UseVector(Self);
end;

{ TQuaternion }

procedure TQuaternion.UseVector(var V: TVector);
begin

end;

For the particular cases the following pattern may be useful:

type
  TVectorData = array [0..1] of Double;
  TQuaternionData = array [0..3] of Double;

  TVector = record
    Data: TVectorData;
    procedure UseQuaternion(var Q: TQuaternionData);
  end;

  TQuaternion = record
    Data: TQuaternionData;
    procedure UseVector(var V: TVector);
  end;



回答3:


I think that your "good old-fashioned" solution is the better.
as an alternate solution - but leading to your solution's schema - what about a Record containing your two record and the methods as methods of the main Record:

type  
  TQuaternionVector = record  
    Vector: TVector;  
    Quaternion: TQuaternion;  
    procedure F(V: TVector; Q: TQuaternion);    
  end;



回答4:


For the specific case of wanting to use operator overloading (and even more specifically, binary operators), the record types being operated on can be specified in either order.

The declarations below will allow you to add vectors and quaternions (if that makes any sense :-) ) in any combination (the 3rd Add declaration for TQuaternion is the interesting one):

type
  TVector = record
    class operator Add( v1, v2 : TVector ) : TVector;
    end;

  TQuaternion = record
    class operator Add( q1, q2 : TQuaternion ) : TQuaternion;
    class operator Add( q : TQuaternion; v : TVector ) : TQuaternion;
    class operator Add( v : TVector; q : TQuaternion ) : TQuaternion;
    end;

Assuming appropriate variable declarations the following all compile:

q1 := q2 + q3;
v1 := v2 + v3;

q1 := q2+v2;
q2 := v2+q2;

Is this enough to cover what you need?



来源:https://stackoverflow.com/questions/2420650/cross-reference-between-delphi-records

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!