本文介绍了什么c ++没有显示错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我上课了 A级 { 私人: int _mem; public: void foo(A a) { _mem = 0; _a.mem = 0; //没有显示访问viloation错误 } }; 在上面的函数中,我通过 a._mem访问对象'的私有成员。为什么c ++不限制私人会员访问 成员fn 即使对象不同? 有没有具体原因是什么? 解决方案 因为foo是A的成员函数。内部成员函数你是允许访问任何实例上的私有成员的 class(不是 只在''this''实例上)。 - Leandro TC Melo 因为foo是A的成员函数。内部成员函数你是允许访问任何实例上的私有成员的 class(不是 只在''this''实例上)。 - Leandro TC Melo 我想知道为什么c ++允许它。是否有任何特殊原因 这个? }; 因为foo是A的成员函数。内部成员函数你是允许访问任何实例上的私有成员的 class(不是 只在''this''实例上)。 - Leandro TC Melo 请注意,你在函数foo中有一些拼写错误...(_a.mem应该是 a._mem) - Leandro TC Melo Hi, I''ve a class class A{private:int _mem;public:void foo(A a){_mem = 0;_a.mem = 0;//not showing access viloation error} }; In the above function I''m accessing object a''s private member bya._mem. Why c++ is not restricting private member access inside amember fneven if the object is different? Is there any specific reason for this? 解决方案 Because foo is a member function of A. Inside member functions you''reallowed to access private members on any instance of the class (notonly on the ''this'' instance).--Leandro T. C. Melo Because foo is a member function of A. Inside member functions you''reallowed to access private members on any instance of the class (notonly on the ''this'' instance).--Leandro T. C. MeloI would like to know why c++ allows it. Is there any particular reasonfor this? Because foo is a member function of A. Inside member functions you''reallowed to access private members on any instance of the class (notonly on the ''this'' instance).--Leandro T. C. Melo Notice that you have some typos in function foo... (_a.mem should bea._mem)--Leandro T. C. Melo 这篇关于什么c ++没有显示错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-30 09:17