CL屁股A { 朋友A级; 也不需要。测试一下。当友谊被授予时,访问 不是必需的。如果A将友谊授予B函数, 则无需访问B函数。 受保护: B(){} ~B(){} }; A级 { 朋友B :: B(); 公开: A(){} ~A(){} }; //// ////////////////////// 方式2: class B { public: B(){} ~B(){} }; A级 { 朋友B :: B(); public: A(){} ~A(){} }; V - 请在通过电子邮件回复时删除资金'A' 我没有回复最热门的回复,请不要问 Victor Bazarov写道: Barry写道: > MiG写道: > ;>您好, 请考虑以下代码段: B类 {受保护: B(){} ~B(){} }; A类 {朋友B:B(); 拼写错误:= :: >> public: A(){} ~A(){} }; 编译此代码会产生以下错误: 错误C2248:''B :: B'':无法访问类''B'中声明的受保护成员' 您错误地错误这意味着您无法访问B :: B A(当你给予朋友时),因为B :: B受到保护 >>为什么我不能给B'' s构造函数特权访问A? 有两种方法可以解决这个 第一个方式是一个不必要的复杂版本的 第二个方式,这意味着只有一种方式 - 只需将''A''的友情授予''''' B :: B()''和那就是全部。 >方式1: A类; 不需要。 > B级 {朋友类一个; 也不需要。测试一下。当友谊被授予时,访问 不是必需的。如果''A''将友谊授予''B''的功能, 它不必访问''B''的功能。 嗯,我的编译器(msvc8.0)再次讨厌程序 我尝试msvc6.0和icl 9.1都工作 Hello,Consider the following code snippet:class B{protected:B() {}~B() {}};class A{friend B:B();public:A() {}~A() {}};Compiling this code produces the following error:error C2248: ''B::B'' : cannot access protected member declared in class ''B''Why can I not give B''s constructor privileged access to A? 解决方案 MiG wrote:Hello,Consider the following code snippet:class B{protected:B() {}~B() {}};class A{friend B:B();typo : =::>public:A() {}~A() {}};Compiling this code produces the following error:>error C2248: ''B::B'' : cannot access protected member declared in class ''B''you got the error wrongIt means you can''t access B::B within A(when you''re granting friend),because B::B is protected>Why can I not give B''s constructor privileged access to A?There are two ways to solve thisway 1:class A;class B{friend class A;protected:B() {}~B() {}};class A{friend B::B();public:A() {}~A() {}};//////////////////////////way 2:class B{public:B() {}~B() {}};class A{friend B::B();public:A() {}~A() {}};Barry wrote:MiG wrote:>Hello,Consider the following code snippet:class B{protected:B() {}~B() {}};class A{friend B:B();typo : =::>>public:A() {}~A() {}};Compiling this code produces the following error:>>error C2248: ''B::B'' : cannot access protected member declared inclass ''B''you got the error wrongIt means you can''t access B::B within A(when you''re granting friend),because B::B is protected>>Why can I not give B''s constructor privileged access to A?There are two ways to solve thisThe first "way" is an unnecessarily complicated version of thesecond "way", which means there is only one way - just grant thefriendship of ''A'' to ''B::B()'' and that''s all.>way 1:class A;No need.>class B{friend class A;No need either. Test it. When friendship is granted, accessis not required. If ''A'' grants friendship to a function of ''B'',it does not have to have access to that function of ''B''.protected:B() {}~B() {}};class A{friend B::B();public:A() {}~A() {}};//////////////////////////way 2:class B{public:B() {}~B() {}};class A{friend B::B();public:A() {}~A() {}};V--Please remove capital ''A''s when replying by e-mailI do not respond to top-posted replies, please don''t askVictor Bazarov wrote:Barry wrote:>MiG wrote:>>Hello,Consider the following code snippet:class B{protected:B() {}~B() {}};class A{friend B:B();typo : =::>>public:A() {}~A() {}};Compiling this code produces the following error:error C2248: ''B::B'' : cannot access protected member declared inclass ''B''you got the error wrongIt means you can''t access B::B within A(when you''re granting friend),because B::B is protected>>Why can I not give B''s constructor privileged access to A?There are two ways to solve thisThe first "way" is an unnecessarily complicated version of thesecond "way", which means there is only one way - just grant thefriendship of ''A'' to ''B::B()'' and that''s all.>way 1:class A;No need.>class B{friend class A;No need either. Test it. When friendship is granted, accessis not required. If ''A'' grants friendship to a function of ''B'',it does not have to have access to that function of ''B''.Well, my compiler(msvc8.0) hates the program againI try msvc6.0 and icl 9.1 both work 这篇关于受保护的会员朋友的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!