C++——友元 friend

人类社会的friend VS C++世界的friend

现实世界中,我们自己很多物品,朋友是可以使用的,但是陌生人就不行。那么money,朋友可以随便拿吗?这要是你和friend的关系深浅而定。人类社会friend多多少少还是会有些限制。而C++中的friend才是真正的friend,class中的money(这里指private data可以随便拿)。用法如下

C++ friend具有如下特性:

  1. friend不是类的成员,但是可以访问class所有成员
  2. friend不受access level控制

friend机制无疑打破了C++一直强调的封装,这种特性还是少用的好。

相同class的各个object互为friends

为啥重载<<常常设计成friend

  老实说,我本身就觉得这种问法很奇怪。难道不设计成class member function,就一定是friend吗?当然不是,你还可以设计成global function啊。当然global function和friend都比class member function要好,为啥?稍后介绍。global function 不如 friend的地方也就是访问private data时,global要通过class“暴露”出来的接口进而访问private data,而friend可以直接拿class 的private data。这在效率上能带来一定的提升。但是在强调封装性的C++中,friend显然是规则破坏者,因此本人更倾向于global 的实现方式。最上面那张图就是global的实现方式。

  再说为啥无论global function 还是 friend 都比 class member function好。代码是写给人看的,无论何时都要记得这一点。我们使用cout的时候都是cout<<selfdef_val,而不是seldef_val<<cout。如果设计成class member function就得采用后面这种“非人类”的调用方式。测试代码如下

#include <iostream>

class Complex {
private:
	int real;
	int img;
public:
	Complex(int _real = 0, int _img = 0) :real(_real), img(_img) {}
	std::ostream& operator<<(std::ostream& out) {
		out << "(" << this->real << "," << this->img << ")";
		return out;
	}
};

#if 0
ostream& operator<<(ostream& out, const Complex &c) {
	out << "(" << c.m_real << "," << c.m_imag << ")";
	return out;
}
#endif

int main() {
	Complex C1(1, 2);
	C1 << std::cout << std::endl;//cout << c << endl;
	return 0;
}

  

 

原文地址:https://www.cnblogs.com/kelamoyujuzhen/p/9444089.html