需要朋友的功能 [英] need for friend function

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

问题描述

Stroustrup,在他的书TC ++ PL第3版,第16页,在

部分''1.8 Advice''中提到了以下内容:

[ 2] [a]不要使用全球数据(使用会员)

[b]不要使用全局功能

[c]不要使用公共数据成员。

[d]不要使用朋友,除了避免[a]或[c]。


请考虑以下情况:


场景1:

----------------

class Test

{

朋友ostream& operator<(ostream& os,const Test& ref);

}


这里的朋友ostream& operator<<()可以通过提供一个

Test :: print_output()函数来避免,并期望用户显式调用类型为Test的Test_obj对象
Test_obj.print_output(os)。


场景2:

----------------

class Test

{

朋友测试操作员+(const测试& arg1,const测试& arg2);

}


这个朋友的功能可以避免如下:


class Test

{

Test& operator + =( const test& ref);

}


测试操作符+(const测试& arg1,const& arg2)

{

测试温度(arg1);

返回温度+ = arg2;

}


场景3:

----------------

考虑反转(m)将矩阵m反转为替代方案

m.invert ()

通过提供Matrix :: invert(),这里的朋友函数也可以避免



我想要的要知道是否有一些准则和例子,

需要使用朋友功能是不可避免的。


请解释一下。

谢谢

V.Subramanian

Stroustrup, in his book TC++PL 3rd Edition, in page 16, under the
section ''1.8 Advice'' has mentioned the following:
[2] [a] Don''t use global data(use members)
[b] Don''t use global functions
[c] Don''t use public data members.
[d] Don''t use friends, except to avoid [a] or [c].

Consider the following scenarios:

Scenario 1:
----------------
class Test
{
friend ostream & operator<<(ostream &os, const Test &ref);
}

Here the friend ostream &operator<<() can be avoided by providing a
Test::print_output() function and expect the user to explicitly call
Test_obj.print_output(os) for an object Test_obj of type Test.

Scenario 2:
----------------
class Test
{
friend Test operator+(const Test &arg1, const Test &arg2);
}

This friend function can be avoided as follows:

class Test
{
Test &operator+=(const Test &ref);
}

Test operator+(const Test &arg1, const &arg2)
{
Test temp(arg1);
return temp += arg2;
}

Scenario 3:
----------------
Consider invert(m) for inverting a ''Matrix m'' to the alternative
m.invert()
By providing Matrix::invert(), here also the friend function can be
avoided.

I want to know if there are some guidelines and examples where the
need for using a friend function is unavoidable.

Kindly explain.

Thanks
V.Subramanian

推荐答案

su ************** @ yahoo.com 写道:

[..]

我想知道是否有一些指南和示例,

需要使用友元函数是不可避免的。
[..]
I want to know if there are some guidelines and examples where the
need for using a friend function is unavoidable.



这就是事情:缺乏证据不是缺席的证据。

如果我从未遇到过我的一生指南和/或

使用朋友功能的例子是不可避免的,它没有

意味着没有。但是,请考虑一下:如果有更好的方式做某事,而你没有理由*不*使用一个更好的方式,那么不太好机制是可以避免的。

反转,你得到这个:如果有令人信服的理由不是

使用任何方法来避免某些东西,那么这个东西

变得不可避免。如果由于某种原因你无法向所有人授予访问某些私人数据的权利(声明数据公开或

受保护),*和*你不能使某些功能成为包含私人数据的

类,那么你就不得不把这个函数作为朋友制作

了。我知道,这不是具体的,但那是'

精华,AFA我很担心。


V

-

请在通过电子邮件回复时删除资金''A'

我没有回复最热门的回复,请不要'请问

Here is the thing: absence of evidence is not evidence of absence.
If I''ve never encountered in my whole life any guidelines and/or
examples where using a friend function is unavoidable, it does not
mean there isn''t any. However, consider this: if there are better
ways of doing something, and you have no reason for *not* using one
of the better ways, then the "not so good" mechanism is avoidable.
Invert that, and you get this: if there are compelling reasons not
to use any of the ways to avoid something, then this something
becomes unavoidable. If for some reason you cannot grant access
to some private data to everybody (declare the data public or
protected), *and* you cannot make certain function a member of the
class containing the private data, then you are stuck with making
the function a friend. I know, it''s not concrete, but that''s the
essence, AFA I''m concerned.

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask


12月4日下午3:56,subramanian10 ... @ yahoo.com,India

< subramanian10 ... @ yahoo.comwrote:
On Dec 4, 3:56 pm, "subramanian10...@yahoo.com, India"
<subramanian10...@yahoo.comwrote:

Stroustrup,在他的书TC ++ PL第3版,第16页,

节''1.8建议''提到了以下内容:

[2] [a]不要使用全球数据(使用会员)

[b] Don不使用全局功能

[c]不要使用公共数据成员。

[d]不要使用朋友,除了避免[a]或者[c]。

考虑以下场景:


场景1:

----- -----------

班级考试

{

朋友ostream& operator<(&ostream& os,const Test& ref);


}


这里有朋友ostream& operator< <()可以通过提供一个
Test :: print_output()函数来避免,并期望用户显式调用对象Test_obj的
Test_obj.print_output(os)类型测试。


场景2:

----------------

班测试

{

朋友测试操作员+(const测试& arg1,const测试& arg2);


}


这个朋友的功能可以避免如下:


class Test

{

Test& operator + =(const Test& ref);


}


测试运算符+(const Test& arg1,const & arg2)

{

测试温度(arg1);

返回温度+ = arg2;


}


场景3:

----------------

考虑反转(m)将矩阵m反转为替代方案

m.invert()

通过提供Matrix :: invert(),这里也可以避免朋友函数

。 br />

我想知道是否有一些指导和示例,

需要使用朋友功能是不可避免的。


请解释一下。
Stroustrup, in his book TC++PL 3rd Edition, in page 16, under the
section ''1.8 Advice'' has mentioned the following:
[2] [a] Don''t use global data(use members)
[b] Don''t use global functions
[c] Don''t use public data members.
[d] Don''t use friends, except to avoid [a] or [c].

Consider the following scenarios:

Scenario 1:
----------------
class Test
{
friend ostream & operator<<(ostream &os, const Test &ref);

}

Here the friend ostream &operator<<() can be avoided by providing a
Test::print_output() function and expect the user to explicitly call
Test_obj.print_output(os) for an object Test_obj of type Test.

Scenario 2:
----------------
class Test
{
friend Test operator+(const Test &arg1, const Test &arg2);

}

This friend function can be avoided as follows:

class Test
{
Test &operator+=(const Test &ref);

}

Test operator+(const Test &arg1, const &arg2)
{
Test temp(arg1);
return temp += arg2;

}

Scenario 3:
----------------
Consider invert(m) for inverting a ''Matrix m'' to the alternative
m.invert()
By providing Matrix::invert(), here also the friend function can be
avoided.

I want to know if there are some guidelines and examples where the
need for using a friend function is unavoidable.

Kindly explain.



A级;

B级;

C级


C foo(A a,B b){

//我需要访问A,B和C的私人成员。

//我必须是朋友其中至少有两个。

};


问候,

FM。


class A;
class B;
class C

C foo(A a,B b){
//I need to access private members of A,B and C.
//I must be a friend of at least two of them.
};

regards,
FM.


12月4日下午2:23,终结者< farid.mehr ... @ gmail.comwrote:
On Dec 4, 2:23 pm, terminator <farid.mehr...@gmail.comwrote:

On 12月4日下午3:56,subramanian10 ... @ yahoo.com,印度


< subramanian10 ... @ yahoo.comwrote:
On Dec 4, 3:56 pm, "subramanian10...@yahoo.com, India"

<subramanian10...@yahoo.comwrote:

Stroustrup,在他的书TC ++ PL第3版,第16页,

部分''1.8 Advice''中提到了以下内容:

[2] [a]不要使用全局数据(使用成员)

[b]不要使用全局函数

[ c]不要使用公共数据成员。

[d]不要使用朋友,除了避免[a]或[c]。
Stroustrup, in his book TC++PL 3rd Edition, in page 16, under the
section ''1.8 Advice'' has mentioned the following:
[2] [a] Don''t use global data(use members)
[b] Don''t use global functions
[c] Don''t use public data members.
[d] Don''t use friends, except to avoid [a] or [c].


考虑以下场景:
Consider the following scenarios:


场景1:

----------------

班级考试

{

朋友ostream& operator<<(ostream& os,const Test& ref);
Scenario 1:
----------------
class Test
{
friend ostream & operator<<(ostream &os, const Test &ref);


}
}


这里的朋友ostream& operator<< ;()可以通过提供一个

Test :: print_output()函数来避免,并期望用户显式调用

Test_obj.print_output(os)来获取一个对象Test_obj类型测试。
Here the friend ostream &operator<<() can be avoided by providing a
Test::print_output() function and expect the user to explicitly call
Test_obj.print_output(os) for an object Test_obj of type Test.


场景2:

----------------

class测试

{

朋友测试操作符+(const测试& arg1,const测试& arg2);
Scenario 2:
----------------
class Test
{
friend Test operator+(const Test &arg1, const Test &arg2);


}
}


这个朋友的功能可以避免如下:
This friend function can be avoided as follows:


class Test

{

Test& operator + =(const Test& ref );
class Test
{
Test &operator+=(const Test &ref);


}
}


测试运算符+(const Test& arg1, const& arg2)

{

测试温度(arg1);

返回温度+ = arg2;
Test operator+(const Test &arg1, const &arg2)
{
Test temp(arg1);
return temp += arg2;


}
}


场景3:

----------------

考虑反转(m)将矩阵m反转为替代值

m.invert()

通过提供Matrix :: invert(),这里也可以避免朋友函数


Scenario 3:
----------------
Consider invert(m) for inverting a ''Matrix m'' to the alternative
m.invert()
By providing Matrix::invert(), here also the friend function can be
avoided.


我想知道是否有一些指南和示例,其中

需要使用友元函数是不可避免的。
I want to know if there are some guidelines and examples where the
need for using a friend function is unavoidable.


请解释一下。
Kindly explain.



A级;

B级;

C级


C foo(A a,B b){

//我需要访问A,B和C的私人成员。

//我必须是朋友其中至少有两个。


};


问候,

FM。


class A;
class B;
class C

C foo(A a,B b){
//I need to access private members of A,B and C.
//I must be a friend of at least two of them.

};

regards,
FM.



大多数情况下你可以使用公共方法......

Most of the time you can use public methods instead...


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

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