这应该算是C++的一个不足吧,C++是优秀的编程语言,但不是完美的。
#include<iostream.h> class A { public : A():_personalA(100){ cout << " this is construction of A " << endl ; print( ); } ~A(){ cout << " this is destruction of A " << endl ; print() ; } int & GetA() { return _personalA ; } void print(){ cout << " this is print() of class A the values of _personalA is : " << _personalA << endl ; } private : int _personalA ; }; int main() { A a ; int &num = a.GetA(); a.print(); cout << num << endl ; cout << " please input the values of _personalA . Attempt to modify the provate variables " << endl; cin >> num ; a.print(); cout << num << endl ;; return 0 ; }C++中私有变量的值想怎么改就怎么改
最新推荐文章于 2021-11-15 21:05:18 发布
本文探讨了C++中如何通过引用返回私有成员变量,实现了外部对私有变量的安全修改,并展示了完整的代码实例。
1万+

被折叠的 条评论
为什么被折叠?



