为什么在 C++/CLI 中定义引用句柄而不是“指针重载”?
我目前正在研究一些 C++/CLI,并且很好奇为什么存在引用句柄而不是“重载指针”。我所说的“重载指针”是指 Visual C++ 2010 编译器推断的 * 指针表示法的使用与 C++ 指针不同。
例如,在标准主标头中:
int main(array<System::String ^>^ args)
我知道 ^ 尾随右尖括号的原因,如 MSDN 上所述。
我知道 String 是 .NET 对象类型,因此是一个引用,但我习惯了 Objective-C,其中对对象的引用采用标准指针表示法:
NSString * string = ... etc.
谢谢,
Scott
I'm working through some C++/CLI at the moment and am curious as to why there are reference handles instead of "overloaded pointers". By "overloaded pointers", I mean the use of the * pointer notation that the Visual C++ 2010 compiler infers being different from C++ pointers.
For example, in the standard main header:
int main(array<System::String ^>^ args)
I am aware of the reason for the ^ trailing the right angle bracket, as outlined on MSDN.
I am aware that String is a .NET object type and is thus a reference, but I am used to Objective-C where references to objects take the standard pointer notation:
NSString * string = ... etc.
Thanks,
Scott
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
因为从技术上讲,引用和指针是不同的。引用是不透明的指针,由运行时管理;指针是不可验证的并且由程序员管理。对两者使用相同的语法可能是可能的,但可能会产生很多混乱和潜在的错误。
Because technically, references and pointers are different. References are opaque pointers, managed by the runtime; pointers are unverifiable and managed by the programmer. Using the same syntax for both would probably have been possible but could have produced a lot of confusion and potential bugs.
@Ben Voigt:在什么情况下系统可以根据类型解析设置器?就我个人而言,我认为没有理由不能(恕我直言,应该允许有一个带有 Complex 类型的 getter、“Double”类型的 setter 和另一个“Complex”类型的 setter 的属性“foo” ;当然可以对 setter 方法进行多次重写,但我知道属性没有这样的功能)。
@Ben Voigt: In what circumstance can the system resolve setters based on type? Personally, I see no reason it shouldn't be able to (IMHO, it should be permissible to have a property "foo" with a getter of type Complex, a setter of type "Double", and another setter of type "Complex"; certainly one could have multiple overrides of a setter method, but I know of no such feature for properties).