Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/joomla/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么我们需要C++;? 我在C++中阅读了朋友关键词,不明白我们为什么需要这样的东西。 例如,我被告知,由于以下功能: bool operator==(const Rational& r1, const Rational&r2);_C++_Friend_Friend Function - Fatal编程技术网

为什么我们需要C++;? 我在C++中阅读了朋友关键词,不明白我们为什么需要这样的东西。 例如,我被告知,由于以下功能: bool operator==(const Rational& r1, const Rational&r2);

为什么我们需要C++;? 我在C++中阅读了朋友关键词,不明白我们为什么需要这样的东西。 例如,我被告知,由于以下功能: bool operator==(const Rational& r1, const Rational&r2);,c++,friend,friend-function,C++,Friend,Friend Function,需要访问像r1.numerator这样的私有成员我们应该通过将其声明为Friend来授予其访问权限,但另一方面,我有一个名为GetNumerator()的函数,所以为什么我不能将r1.numerator更改为r1.GetNumerator()?读一读,想大一点。如果您需要在该协作中使用未被公共获取者公开的成员,该怎么办?那时你需要朋友@churill,那时我写更多的getter:-)这能回答你的问题吗?通常,您会使用getter。使用friend的一个原因可能是,如果您有一种方法可以更快地与不存

需要访问像r1.numerator这样的私有成员我们应该通过将其声明为Friend来授予其访问权限,但另一方面,我有一个名为GetNumerator()的函数,所以为什么我不能将r1.numerator更改为r1.GetNumerator()?

读一读,想大一点。如果您需要在该协作中使用未被公共获取者公开的成员,该怎么办?那时你需要
朋友
@churill,那时我写更多的getter:-)这能回答你的问题吗?通常,您会使用getter。使用
friend
的一个原因可能是,如果您有一种方法可以更快地与不存在getter的私有成员变量进行比较,例如,如果您可以基于一些内部知识进行比较,但不想公开,因为这些信息可能会在将来的代码版本中发生变化,因此应该是私有的。