如何在getter,setter中使用属性作为参考参数进行计算 [英] How to use a property as a reference paramter with calculation in getter, setter

查看:160
本文介绍了如何在getter,setter中使用属性作为参考参数进行计算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个需要参考的功能,如

Editfield(BYTE& value,....)

的作品好吧,只要我在getter / setter中使用直接访问BYTE&像返回一样

  __ declspec  property  (put = _SetPropsText,get = _GetPropsText))BYTE propsText; 
void _SetPropsText(BYTE val){((BYTE *)& props_new)[ 1 ] = val; }
BYTE& _GetPropsText(){ return ((BYTE *)& props_new)[ 1 ]; };



但是我完全不理解getter和setter的功能,因为删除了&在getter中失败:

  __ declspec  property (put = _SetPropsText,get = _GetPropsText))BYTE propsText; 
void _SetPropsText(BYTE val){((BYTE *)& props_new)[ 1 ] = val; }
BYTE _GetPropsText(){ return ((BYTE *)& props_new)[ 1 ]。 };



不起作用或打电话!

可以使用以下内容:

< pre lang =c ++> propsText = 12 ;





但从不作为参数调用参考作为参数



我无法解决的问题是,当我在我的getter / setter中使用计算时:

  __ declspec  property (put = _SetPropsTextSize,get = _GetPropsTextSize)) float  propsTextSize; 
void _SetPropsTextSize( const float val){((BYTE *)& props_new)[ 2 ] =(BYTE)(val * 10 ); }
float _GetPropsTextSize() const { return (( float )((BYTE *)& props_new)[ 2 ])/ 10 ; };



然后致电

 EditFloat(float&,...)
EditFloat (propsTextSize,...)





有什么想法可以解决这个问题吗?



我尝试了什么:



我不知道如何克服这个问题,它在C#中有效,但在C ++中却没有?

临时变量无法解决,因为当温度变化将被更改时,不会调用setter



当有=时塞特被叫,但是当有一个& getter被称为

(用debug检查)

解决方案

简短的回答是:你不能也不应该。



getter / setter的目的是让getter / setter代码充当屏障或守门员。一些getter / setter计算返回值或具有多个副作用。可能没有实际的私有变量。



你可以做的是使用局部变量。



  float  local = propsTextSize; 
EditFloat(local,...);
propsTextSize = local;


I have a function that needs a reference as a parameter like
Editfield( BYTE& value, .... )
works OK as long I use direct access in getter /setter with BYTE& as return like

__declspec (property (put = _SetPropsText, get = _GetPropsText)) BYTE propsText;
void _SetPropsText(BYTE val) { ((BYTE*)&props_new)[1] = val; }
BYTE& _GetPropsText() { return ((BYTE*)&props_new)[1]; };


But at all I don´t understand the function of getter and setter because removing the & in the getter fails:

__declspec (property (put = _SetPropsText, get = _GetPropsText)) BYTE propsText;
void _SetPropsText(BYTE val) { ((BYTE*)&props_new)[1] = val; }
BYTE _GetPropsText() { return ((BYTE*)&props_new)[1]; };


does NOT work or function calls!
It is OK using things like:

propsText= 12; 



but never works as a referce in a call as parameter

The problem I cannot resolve is, when I use calculation in my getter/setter like:

__declspec (property (put = _SetPropsTextSize, get = _GetPropsTextSize)) float propsTextSize;
void _SetPropsTextSize(const float val) { ((BYTE*)&props_new)[2] = (BYTE)(val*10); }
float _GetPropsTextSize() const { return ((float)((BYTE*)&props_new)[2]) / 10; };


and then call

EditFloat( float& , ...)
EditFloat( propsTextSize, ...)



any idea to overcome this?

What I have tried:

I have no idea to overcome this, imho it works in C# but not in C++?
A temp var does not solve it becuase the setter is not called when the temp var will be changed

when ever there is a "=" teh Setter is called, but when there is a & the getter is called
(checked with debug)

解决方案

Short answer is: you can't and shouldn't.

The purpose of getter / setter is to have the getter / setter code act as barrier or gatekeeper. Some getter / setters compute a return value or have multiple side effects. There might not be an actual private variable.

What you can do is work with a local variable.

float local = propsTextSize;
EditFloat( local, ...);
propsTextSize = local;


这篇关于如何在getter,setter中使用属性作为参考参数进行计算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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