C++/CLI shorthand properties

你离开我真会死。 提交于 2019-12-05 09:28:09

问题


How does a developer do the equivalent of this in managed c++? :

c# code

public String SomeValue
{
  get;
  set;
}

I've scoured the net and found some solutions, however it is hard to distinguish which is the correct (latest, .NET 3.5) way, given the colourful history of getters/setters and managed c++.

Thanks!


回答1:


Managed C++ does not support automatic properties. You should manually declare a backing field and the accessors:

private: String* _internalSomeValue;
public:
__property String* get_SomeValue() { return _internalSomeValue; }
__property void set_SomeValue(String *value) { _internalSomeValue = value; }

C++/CLI supports automatic properties with a very simple syntax:

public: property String^ SomeValue;

Update (reply to comment):

In C++/CLI, you cannot control the accessibility of each accessor method separately when you use the automatic property syntax. You need to define the backing field and the methods yourself:

private: String^ field;
property String^ SomeValue { 
   public: String^ get() { return field; }
   private: void set(String^ value) { field = value; }
}



回答2:


In C++/CLI you would do just:

property String^ SomeValue;



回答3:


Just to give you more search terms, this is called a trivial property



来源:https://stackoverflow.com/questions/1907814/c-cli-shorthand-properties

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!