0

There is a form Form1 which contains int x; and a class MyClass with .h and .cpp files. How can I access the Form1 variable x from MyClass, without passing it using a method? I think something like Form1.x Maybe is it a bad style of programming? I am just learning..

4

1 回答 1

1

好吧,它打破了封装,你真的应该有你想要公开的数据的公共 getter 和 setter,允许像你建议的那样访问允许调用者损坏、重新分配等。基本上做他们喜欢的任何事情。您应该添加一种方法getX()以获取更好的名称。

C++/CLI 支持的属性可能是您所追求的,请参阅 msdn 文章:http: //msdn.microsoft.com/en-us/library/ms379617%28v=vs.80%29.aspx#vs05cplus_topic10和代码项目帖子:http://www.codeproject.com/Articles/10352/C-CLI-Properties-Syntactic-sugar-for-accessor-meth

于 2012-04-21T21:22:21.760 回答