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 }; 

    3 answers 3

    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.

    • > 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). As far as I know, .NET languages ​​do not have to support properties. Properties implicitly generate functions get_Property and set_Property. In addition, the properties in VC ++ work without using C ++ \ CLI. - gammaker
    • @GLmonster Understandably, that is not necessary. However, if we want an object with properties to be generated based on 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 Rupert
    • > However, if we want an object with properties to be generated based on C ++ CLI code (for example, to access these properties from the Sharp code), then we simply have no way out except using __property. In practice, I have not tried it, but theoretically you can, if you call the functions get_Value and set_Value, and in C # refer to as Value. - gammaker
    • @GLmonster Well, in the MSDN example, such calls are marked with __property . I do not know about without __property , but it definitely works with him. Somehow check. - Costantino Rupert

    This 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).

      • @BuilderC, is it possible in more detail? I haven't seen anything like this. Are these some compiler plugins or common stuff? - skegg
      • More in detail: A.Ya.Arkhangelsky, "Programming in C ++ Builder 6", M., "Binom", 2002, p.524. - BuilderC
      • Yes, yes, I lay such a brick. Is this his characteristic features? - skegg
      • Not really "exactly the builder." Recall that in C ++ there are possible functions that return a reference to a variable, and which, therefore, can be used in the form F (x) = n, i.e. in the assignment operator to put on the left. It seems to me that they are used here. However, it would be well respected @ Kitty-wants-eat ask: he will be more educated than me. - BuilderC
      • @BuilderC You're welcome) - Costantino Rupert