朋友和Visual C ++ Express 2005 [英] Friends and Visual C++ Express 2005

查看:79
本文介绍了朋友和Visual C ++ Express 2005的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Lippman的C ++ Primer,第4版,p575说:


友谊不是继承的。基类的朋友没有

对其派生类成员的特殊访问权限。如果基类是

授予友谊,则只有基类具有特殊访问权限。从该基础派生的类
无法访问授予友谊的类。


我在Visual C ++ 2005 Express中试过这个,它似乎只是

实现上面友谊规则的一半。


示例代码:


========= ========================================


class A2;


class A

{

朋友B级;

朋友无效g(A,A2);

私人:

int x;

};


A2级:公共A

{

私人:

int x2;

};


B级

{

公开:

void f(A a){ax; }


void f2(A2 a2){a2.x; a2.x2;}

// a2.x编译,但a2.x2给出编译错误

// f2中的两个语句都应该是编译错误???

};


B2级:公共B

{

公开:

// void f(A a){ax; } //给出编译错误,因为它应该

};


void g(A a,A2 a2)

{

ax; //编译

a2.x; //编译,但不应该吗?

a2.x2; //不编译

}


int main()

{

}


====================================== ===========


Visual C ++ 2005 Express没有正确实现友谊规则吗?

Lippman''s C++ Primer, 4th ed., p575 says:

Friendship is not inherited. Friends of the base class have no
special access to members of its derived class. If a base class is
granted friendship, only the base has special access. Classes derived
from that base have no access to the class granting friendship.

I tried this in Visual C++ 2005 Express, and it seems to only
implement half of the friendship rules above.

Example code:

=================================================

class A2;

class A
{
friend class B;
friend void g(A, A2);
private:
int x;
};

class A2 : public A
{
private:
int x2;
};

class B
{
public:
void f(A a) { a.x; }

void f2(A2 a2) { a2.x; a2.x2;}
// a2.x compiles, but a2.x2 gives compile error
// both statements in f2 should be compile errors ???
};

class B2 : public B
{
public:
// void f(A a) { a.x; } // gives compile error, as it should
};

void g(A a, A2 a2)
{
a.x; // compiles
a2.x; // compiles, but shouldn''t ?
a2.x2; // doesn''t compile
}

int main()
{
}

=================================================

Is Visual C++ 2005 Express not implementing friendship rules correctly?

推荐答案

t写道:
t wrote:

Lippman的C ++ Primer,第4版,p575说:


友谊不是继承的。基类的朋友没有

对其派生类成员的特殊访问权限。如果基类是

授予友谊,则只有基类具有特殊访问权限。从该基础派生的类
无法访问授予友谊的类。


我在Visual C ++ 2005 Express中试过这个,它似乎只是

实现上面友谊规则的一半。


示例代码:


========= ========================================


class A2;


class A

{

朋友B级;

朋友无效g(A,A2);

私人:

int x;

};


A2级:公共A

{

私人:

int x2;

};


B级

{

公开:

void f(A a){ax; }


void f2(A2 a2){a2.x; a2.x2;}

// a2.x编译,但a2.x2给出编译错误

// f2中的两个语句都应该是编译错误???

};


B2级:公共B

{

公开:

// void f(A a){ax; } //给出编译错误,因为它应该

};


void g(A a,A2 a2)

{

ax; //编译

a2.x; //编译,但不应该吗?

a2.x2; //不编译

}


int main()

{

}


====================================== ===========


Visual C ++ 2005 Express无法正确实现友谊规则吗?
Lippman''s C++ Primer, 4th ed., p575 says:

Friendship is not inherited. Friends of the base class have no
special access to members of its derived class. If a base class is
granted friendship, only the base has special access. Classes derived
from that base have no access to the class granting friendship.

I tried this in Visual C++ 2005 Express, and it seems to only
implement half of the friendship rules above.

Example code:

=================================================

class A2;

class A
{
friend class B;
friend void g(A, A2);
private:
int x;
};

class A2 : public A
{
private:
int x2;
};

class B
{
public:
void f(A a) { a.x; }

void f2(A2 a2) { a2.x; a2.x2;}
// a2.x compiles, but a2.x2 gives compile error
// both statements in f2 should be compile errors ???
};

class B2 : public B
{
public:
// void f(A a) { a.x; } // gives compile error, as it should
};

void g(A a, A2 a2)
{
a.x; // compiles
a2.x; // compiles, but shouldn''t ?
a2.x2; // doesn''t compile
}

int main()
{
}

=================================================

Is Visual C++ 2005 Express not implementing friendship rules correctly?



编译器是否正确。

the compiler is correct.


>
>



远方的朋友(A) (B,g)不一定是儿子的朋友(A2)。


-

谢谢

巴里


我不确定。我给出的代码中有一行涉及a2.x

,它们正在编译我认为不应该编译。似乎

就像Visual C ++编译器使用更宽松的友谊规则。


Lippman中的例子是不同的。我只需输入它。

请参阅//我的评论:我的评论如下。所有其他评论

是Lippman's。


===================== =============================

class Base {

朋友班Frnd;

受保护:

int i;

};


/ / Frnd无法访问D1中的成员

类D1:公共基地{

受保护:

int j;

};


class Frnd {

public:

int mem(Base b){return bi; } // ok:Frnd是Base的朋友

int mem(D1 d){return d.i; } //错误:友谊没有继承

//我的评论:VC ++编译这个!

};


// D2无法访问Base中的成员

class D2:public Frnd {

public:

int mem(Base b){return双; } //错误:友谊没有

继承

//我的评论:VC ++将此标记为错误!

};


======================================== ========== =======

I''m not sure. There are lines in the code I gave involving "a2.x"
that are compiling that I don''t think should be compiling. It seems
like the Visual C++ compiler is using more lenient friendship rules.

The example in Lippman is different. I''ll just type it out.
See "// my comment:" for my comments below. All other comments
are Lippman''s.

==================================================

class Base {
friend class Frnd;
protected:
int i;
};

// Frnd has no access to members in D1
class D1 : public Base {
protected:
int j;
};

class Frnd {
public:
int mem(Base b) { return b.i; } // ok: Frnd is friend to Base
int mem(D1 d) { return d.i; } // error: friendship doesn''t inherit
// my comment: VC++ compiles this!
};

// D2 has no access to members in Base
class D2 : public Frnd {
public:
int mem(Base b) { return b.i; } // error: friendship doesn''t
inherit
// my comment: VC++ flags this as error!
};

================================================== =======


" t" < tm **** @ Yahoo.comwrote in message

news:11 ********************** @ r29g2000hsg.googlegr oups.com ...

:Lippman的C ++ Primer,第4版,p575说:



:友谊不是遗传。基类的朋友没有

:对其派生类成员的特殊访问权限。如果基类是

:授予友谊,则只有基类具有特殊访问权限。派生的类

:从该基地无法访问授予友谊的类。



:我在Visual C ++ 2005 Express中试过这个,它似乎只有
:实现上面友谊规则的一半。



:示例代码:



:======================================= ==========







:A2级; < br $> b $ b:

:A级

:{

:朋友B级;

:朋友无效g(A,A2);

:私人:

:int x;

:};



:A2级:公共A

:{

:私人:

:int x2;

:};



:B级

:{

:公开:

:void f(A a){ax; }



:void f2(A2 a2){a2.x; a2.x2;}

:// a2.x编译,但a2.x2给出编译错误

:// f2中的两个语句都应该是编译错误???

a2.x没问题,没有继承友谊:

A是A2的* public *基类,所以可以使用a2

作为A类对象(没有友谊)。并且

因为B是A的朋友,B :: f2可以访问A :: x。


:};



:B2级:公共B

:{

:公开:

://无效f(A a){ax; } //给出编译错误,因为它应该

右:这是证明上面引用的Lippman段落中描述的限制




:};



:void g(A a,A2 a2)

:{

:ax; //编译

:a2.x; //编译,但不应该吗?

再次:看到a2属于A类不需要友谊,

因为A是A2的公共基类。


:a2.x2; //不编译

:}



:int main()

:{< br $> b $ b:}



:======================= ==========================



:Visual C ++ 2005 Express不是实施友谊规则

正确吗?


以上代码至少似乎处理得当。

hth -Ivan

-
http:// ivan。 vecerina.com/contact/?subject=NG_POST < - 电子邮件联系表格

Brainbench MVP for C ++< http://www.brainbench.com

"t" <tm****@Yahoo.comwrote in message
news:11**********************@r29g2000hsg.googlegr oups.com...
: Lippman''s C++ Primer, 4th ed., p575 says:
:
: Friendship is not inherited. Friends of the base class have no
: special access to members of its derived class. If a base class is
: granted friendship, only the base has special access. Classes derived
: from that base have no access to the class granting friendship.
:
: I tried this in Visual C++ 2005 Express, and it seems to only
: implement half of the friendship rules above.
:
: Example code:
:
: =================================================
:
:
:
: class A2;
:
: class A
: {
: friend class B;
: friend void g(A, A2);
: private:
: int x;
: };
:
: class A2 : public A
: {
: private:
: int x2;
: };
:
: class B
: {
: public:
: void f(A a) { a.x; }
:
: void f2(A2 a2) { a2.x; a2.x2;}
: // a2.x compiles, but a2.x2 gives compile error
: // both statements in f2 should be compile errors ???
a2.x is ok, without "inheritance of friendship":
A is a *public* base class of A2, so a2 can be used
as an object of type A (without friendship). And
because B is a friend of A, B::f2 has access to A::x.

: };
:
: class B2 : public B
: {
: public:
: // void f(A a) { a.x; } // gives compile error, as it should
Right: this is what demonstrates the limitation
described in Lippman''s paragraph quoted above.

: };
:
: void g(A a, A2 a2)
: {
: a.x; // compiles
: a2.x; // compiles, but shouldn''t ?
Again: seeing that a2 is of type A does not require friendship,
because A is a public base class of A2.

: a2.x2; // doesn''t compile
: }
:
: int main()
: {
: }
:
: =================================================
:
: Is Visual C++ 2005 Express not implementing friendship rules
correctly?

The above code at least seems to be handled correctly.
hth -Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Brainbench MVP for C++ <http://www.brainbench.com


这篇关于朋友和Visual C ++ Express 2005的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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