0

我在 Embarcadero C++Builder XE8 中使用以下代码。

在 Unit1.h 中:

private:    
  void     SetValue (Currency value);   
  Currency FValue;    

public:    
  __property Currency Value = {write= SetValue , read=FValue};    

在 Unit1.cpp 中:

void TForm1::SetValue(Currency _Value)   
{   
  FValue= _Value;   
  Label1->Caption= _Value;   
}   

在我调用的主程序中:

  Value = 10;   // it calls SetValue and the Value will be set to 10   
  Value+= 10;   // it does not call SetValue and Value will not be set to 20   

为什么不Value+= 10调用SetValue(20)

4

1 回答 1

1

您不能将复合赋值运算符与属性一起使用。编译器没有实现,也从来没有。

Value = 10;是直接赋值,因此它按预期调用属性的setter

Value += 10; 不会转化为Value = Value + 10;您所期望的。它实际上调用属性的getter来检索临时值,然后递增并将临时值分配给自身。临时值永远不会分配回属性,这就是不调用属性的设置器的原因。换句话说,它转换为temp = Value; temp += 10;.

要执行您正在尝试的操作,您必须单独显式使用+and=运算符:

Value = Value + 10;
于 2016-02-14T04:54:12.123 回答