Click here to Skip to main content
15,889,808 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
C++
__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:
C++
__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:
C++
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:
C++
__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)
Posted
Updated 28-Apr-16 6:41am
v3
Comments
Malli_S 28-Apr-16 3:47am    
Removing & from return type will not work as you expect. You've to have referenced return type.
FriendOfAsherah 28-Apr-16 4:26am    
so bad luck fo me :-( ;-)
any idea how to overcome this in another way?
type conversion etc?
I need to used these "comperssed" information fields, and cannot change the funtion which is called

1 solution

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.

C++
float local = propsTextSize;
EditFloat( local, ...);
propsTextSize = local;
 
Share this answer
 
v2
Comments
FriendOfAsherah 2-May-16 1:42am    
Im sorry, I forgot to mention:
The function will only _set up_ an Editfield and store the reference inside it.
so local variable wont work cause changes of the value will be done by the "Edit"object any time.
What I need is something like a referenceable property
[no name] 2-May-16 12:26pm    
Your edit object will need to store function pointers to get/set this property.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900