People help! Explain what this code does?
__property int SizeX = { read=FSizeX, write=SetSizeX }; __property int SizeY = { read=FSizeY, write=SetSizeY }; __property int Vy = { read=Fvy, write=SetVy };
People help! Explain what this code does?
__property int SizeX = { read=FSizeX, write=SetSizeX }; __property int SizeY = { read=FSizeY, write=SetSizeY }; __property int Vy = { read=Fvy, write=SetVy };
The question to you, in principle, has already been answered, but I will try to clarify some details.
Generally speaking, __property
is some attempt to increase the standard C ++ features in a platform-specific way.
Specifically, in C ++ Builder, the following code fragments will be equivalent (except in the first case, the responsibility for generating getters / setters is transferred to the compiler ).
__property int SizeX = { read=FSizeX, write=SetSizeX }; public: void SetSizeX(int NewSizeX) { SizeX = NewSizeX; } int FSizeX() const { return SizeX; } private: int SizeX;
Using __property
has only one visible advantage, which is to eliminate the need to write boilerplate code for getters and setters. Well and, plus, we receive some structurization of the code when all property-like members of a class appear in the same place.
The disadvantages are much more - first of all, the complete intolerance of such code to other platforms . It is worth adding here the non-obviousness of generating GetProperty
in terms of constancy, the lack of support for transfer semantics ( &&
) from the C ++ 0x standard, the complexity of writing non-trivial getters and setters that support lazy-loading , etc.
For me personally, for example, the question “to use __property
or not” simply does not arise, since unfortunately, there are many more disadvantages than advantages. The same, incidentally, relates to platform-specific things like _CrtSetReportMode
, which, if used, should be strictly separated by ifdef'ами
.
PS
If you think a bit more about __property
, then a similar extension exists in Microsoft C ++ / CLI. But, unlike C ++ Builder, there the need to use __property
is due to the native support of properties in C # (that is, the developers of the CLI Runtime just needed to somehow duplicate the same mechanism in the C ++ binding).
Pps
Also note that the property-like problem of the members of the class and the need for the corresponding convention are well solved in Google C ++ Code Style, where the way to decorate the members of the class get_value()
is clearly defined.
C++ CLI
code (for example, to access these properties from the Sharp code) , then we simply don’t have an exit other than using __property
. -------- In VC++
, in principle, you can use __declspec(property(...))
to replace getters and setters instead of the usual class.member
call, but this already weakly relates to the question class.member
and, frankly, is another slurred and specific add-on for the language. - Costantino RupertMSDN
example, such calls are marked with __property
. I do not know about without __property
, but it definitely works with him. Somehow check. - Costantino RupertThis is C ++ code. I myself wrote this many times. The developer does not allow the user to access the Fx
fields directly: reading the field through a property, and changing it through the SetX()
function. The field and function are drawn up as private
in the class, and property
as public
.
This code is apparently not in C ++. But it is clear that he declares two three fields and sets, respectively, for each of the get and set methods. This is from the PLO (in case you don’t know what a property is).
Source: https://ru.stackoverflow.com/questions/50945/
All Articles