受保护的会员朋友 [英] Protected member friend

查看:105
本文介绍了受保护的会员朋友的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,


请考虑以下代码片段:


class B

{

受保护:

B(){}

~B(){}

};

A级

{

朋友B:B();


公开:

A(){}

~A(){}

};


编译此代码会产生以下结果错误:


错误C2248:''B :: B'':无法访问类''B'中声明的受保护成员'



为什么我不能给B'的构造函数特权访问A?

解决方案

MiG写道:


您好,


请考虑以下代码段:


B类
{

受保护:

B(){}

~B(){}

};


A级

{

朋友B:B();



拼写错误:= ::


>

public:

A(){}

~A(){}

};


编译这个代码产生以下错误:


>错误C2248:''B :: B'':无法访问在类'''B''中声明的受保护成员$ b



你错了错误

这意味着你不能在A中访问B :: B(当你''重新授予朋友),

,因为B :: B受到保护


>

为什么我不能给B'的构造函数特权访问A?



有两种方法可以解决这个问题


方式1:

A类;


B级

{

朋友A级;

受保护:

B(){}

~B(){}

};


A级

{

朋友B :: B();

公开:

A(){}

~A(){}

};


///////////////////// /////


方式2:


B级

{
$ b $公开:

B(){}

~B(){}

};


class A

{

朋友B :: B();

公开:

A() {}

~A(){}

};


Barry写道:


MiG写道:


>您好,

请考虑以下代码片段:

B班
{
受保护:
B(){}
~B(){}
};
CL屁股A
朋友B:B();



拼写错误:= ::


>>
public:
A(){}
~A(){}
};

编译此代码会产生以下错误:


>>错误C2248:''B :: B'':无法访问
类'B'中声明的受保护成员'



你错了错误

这意味着你不能在A中访问B :: B(当你给予朋友时),

因为B :: B受到保护


>>
为什么我不能给B'的构造函数特权访问A?



有两种方法可以解决这个问题



第一个方式是一个不必要的复杂版本的

第二个方式,这意味着只有一种方式 - 只需将''A''的友情授予''''' B :: B()''和那就是全部。


>

方式1:

A级;



不需要。


>

class B
{

朋友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 wrong
It 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 this

way 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 in
class ''B''


you got the error wrong
It 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 this

The first "way" is an unnecessarily complicated version of the
second "way", which means there is only one way - just grant the
friendship 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, access
is 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-mail
I do not respond to top-posted replies, please don''t ask


Victor 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 in
class ''B''

you got the error wrong
It 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 this


The first "way" is an unnecessarily complicated version of the
second "way", which means there is only one way - just grant the
friendship 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, access
is 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 again

I try msvc6.0 and icl 9.1 both work


这篇关于受保护的会员朋友的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆