C#类属性在本机C + +? [英] C#-like properties in native C++?
问题描述
someThing.text =blah
String blah = someThing.text;但是,上面的代码实际上并没有直接与someThing的文本String交互,它使用了一个get和()函数,设置属性。类似地,可以使用只读属性。
是否有办法在本机 C ++ ? (非C ++ .NET)
解决方案在.NET属性中是真正的 get 和 set
在后台发出的函数(事实上,它们不仅仅是语法糖,因为属性在生成的IL中被发射,并且可以用于反射)。所以在C ++中,你需要明确写这些函数,因为没有属性的概念。
In C# / .NET you can do something like this:
someThing.text = "blah";
String blah = someThing.text;
However, the above code does not actually interact with the someThing's text String directly, it uses a get and set property. Similarly, read-only properties can be used.
Is there a way to do something similar in native C++? (not C++ .NET)
解决方案 In .NET properties are syntactic sugar for the real get
and set
functions which are emitted behind the scenes (in fact they are more than syntactic sugar because properties are emitted in the resulting IL and could be used with Reflection). So in C++ you would need to explicitly write those functions as there's no such notion as property.
这篇关于C#类属性在本机C + +?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!