What are properties in C ++ / CLI?

I saw C ++ code in the term property . I think it is related to C ++ / CLI.

What it is?

+4
source share
2 answers

This is really related to C ++ / CLI (unmanaged C ++ has no concept of properties).

Properties are objects that behave like fields, but are internally handled by the getter and setter access functions. They can be scalar properties (where they behave like a field) or indexed properties (where they behave like an array). In the old syntax, we needed to specify the getter and setter methods directly in our code to implement the properties - this was not as good as you might have guessed. In C ++ / CLI, the syntax is more C # better and easier to write and understand.

Taken from this article: http://www.codeproject.com/KB/mcpp/CppCliProperties.aspx

Also see the MSDN properties in C ++ / CLI.

Code example:

 private: String^ lastname; public: property String^ LastName { String^ get() { // return the value of the private field return lastname; } void set(String^ value) { // store the value in the private field lastname = value; } } 
+3
source

Yes, indeed, this is a version of Microsoft C ++ managed code or C ++ / CLI. Now you not only need to write Get and Set Methods, but you also need to define it as a property. I will say just as I hate adding the extra letters "Read only" and "Write only", the properties of this property are pretty neat.

But superfluous in unmanaged C ++ !!!

For example, you can write in a class (will do the same!):

 std::string GetLastName() const { return lastname;} void SetLastName(std::string lName) { lastname = lName;} 

"const" made sure that "GET" was just read and the set was clear. No need to define a property or add confusion to String ^ vs. std :: string ....

+1
source

Source: https://habr.com/ru/post/1338612/


All Articles