lital maatuk
lital maatuk

Reputation: 6249

What are properties in C++/CLI?

I saw in the term property in C++ code. I think it's connected to C++/CLI.

What is it exactly?

Upvotes: 7

Views: 10052

Answers (2)

Zoul007
Zoul007

Reputation: 603

Yep indeed this is Microsoft's version of managed c++ code or C++/CLI. Now not only do you still have to write Get & Set Methods but you also need to define it as a property. I will say as much as I hate the extra typing the 'Read Only' and 'Write Only' versions of the property is pretty neat.

But unnecessary in un-managed c++!!!

For instance you could write in a class (will do exactly the same thing!):

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

The 'const' made sure it 'GET' was read only, and the set was clear. No need to define a property or add the confusion of String^ vs. std::string....

Upvotes: 0

Cody Gray
Cody Gray

Reputation: 244782

It was indeed connected to C++/CLI (unmanaged C++ doesn't really have a notion of properties).

Properties are entities that behave like fields but are internally handled by getter and setter accessor 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 had to specify the getter and setter methods directly in our code to implement properties - wasn't all that well received as you might guess. In C++/CLI, the syntax is more C#-ish and is easier to write and understand.

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

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

Sample code:

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

Upvotes: 6

Related Questions