在分配“增强的记录"时,我该重载什么运算符?到正常的“数据类型"多变的? [英] What operator do I overload when assigning an "Enhanced Record" to a normal "Data Type" variable?

查看:89
本文介绍了在分配“增强的记录"时,我该重载什么运算符?到正常的“数据类型"多变的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我需要知道我尝试做的事情是否可能.如果可能的话,我就需要知道如何.

证明问题而不是解释问题要容易得多,

我有一个增强记录"(目的-尽管对这个问题并不重要-的目的是产生智能字符串"类型,以替换普通的字符串类型):

TLKString = record
  Value: String;
  // Some methods here to operate on and build String values

  // Allows me to assign String values directly to "instances" 
  // of this record type! I have others (hence "overload") to 
  // handle other data types (such as Integer etc.)
  class operator Implicit(const AValue: String): TLKString; overload; 
end;

我现在可以按如下方式使用此TLKString类型:

var
  LSmartString: TLKString;
begin
  LSmartString := 'Hello World'; // The "Implicit" operator then 
                                 // assigns this to LSmartString.Value
end;

好的,到目前为止,一切都很好!现在我们解决了这个问题...

我需要能够将LSmartString的值(TLKString的实例")分配给普通的String变量...

var
  LSmartString: TLKString;
  LNormalString: String;
begin
  LSmartString := 'Hello World';

  // The next line works fine, but is not what I want!
  LNormalString := LSmartString.Value; 
  LNormalString := LSmartString; // E2010 (Incompatible Types)
end;

这就是我要解决的地方,因为(我敢肯定,您会注意到),上述片段的最后一行导致产生 E2010不兼容类型:'string'和'TLKString'.我当然知道会发生这种情况...我不知道是否可以通过将TLKString记录类型上的运算符重载来克服,如果可以,那么我需要重载什么运算符来做到这一点. /p>

如果这不可能,那么让我感到不高兴的是CodeGear和Embarcadero,让ImplicitExplicit运算符来处理将值分配给增强的Record类型,而没有运算符来处理逆向

解决方案

好的,我已经回答了这个问题……这就是我所说的非常明显".

这是解决方案(为了他人的利益)

TLKString = record
  Value: String;
  // Some methods here to operate on and build String values
  class operator Implicit(const AValue: String): TLKString; overload; // handles String to TLKString assignment
  class operator Implicit(const AValue: TLKString): String; overload; // handles TLKString to String assignment! THIS IS THE ANSWER!
end;

I need to know, first and foremost, if what I'm trying to do is even possible. If it is possible, I then need to know how.

It's far easier to demonstrate the problem rather than explain it so here goes:

I have an "Enhanced Record" (the purpose - though not important to this question - is to produce a "Smart String" type, to replace the normal String type):

TLKString = record
  Value: String;
  // Some methods here to operate on and build String values

  // Allows me to assign String values directly to "instances" 
  // of this record type! I have others (hence "overload") to 
  // handle other data types (such as Integer etc.)
  class operator Implicit(const AValue: String): TLKString; overload; 
end;

I can now use this TLKString type as follows:

var
  LSmartString: TLKString;
begin
  LSmartString := 'Hello World'; // The "Implicit" operator then 
                                 // assigns this to LSmartString.Value
end;

Okay, so far everything is great! Now we get to the problem...

I need to be able to assign the value of LSmartString (an "instance" of TLKString) to a normal String variable...

var
  LSmartString: TLKString;
  LNormalString: String;
begin
  LSmartString := 'Hello World';

  // The next line works fine, but is not what I want!
  LNormalString := LSmartString.Value; 
  LNormalString := LSmartString; // E2010 (Incompatible Types)
end;

This is where I come unstuck, as (I'm sure you'll notice), the last line of the above snip results in E2010 Incompatible types: 'string' and 'TLKString'. I, of course, knew this would be the case... what I don't know if it's possible to overcome by overloading an operator on my TLKString record type, and if so, what operator I need to overload to do it.

If this isn't possible, then it strikes me as a little silly of CodeGear and Embarcadero to have Implicit and Explicit operators to handle assignment of values to an enhanced Record type, but no operator to handle the inverse.

解决方案

Okay, I've answered this myself... and it was what I would call "blindingly obvious".

Here's the solution (for the benefit of others)

TLKString = record
  Value: String;
  // Some methods here to operate on and build String values
  class operator Implicit(const AValue: String): TLKString; overload; // handles String to TLKString assignment
  class operator Implicit(const AValue: TLKString): String; overload; // handles TLKString to String assignment! THIS IS THE ANSWER!
end;

这篇关于在分配“增强的记录"时,我该重载什么运算符?到正常的“数据类型"多变的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆