## 一.友元
1. **功能** 讓其他類和函數訪問類的非公有成員。
2. **友元的聲明** 將函數或類或類的成員函數聲明放入類,并在聲明左邊加上 `friend`
>[warning] 以下例子可在 除VC++6.0 外的編譯器下運行
```c++
#include <iostream>
using namespace std;
class Complex
{
private:
double real_;
double imaginary_;
public:
Complex() : real_(0), imaginary_(0)
{
}
Complex(double real) : real_(real), imaginary_(0)
{
}
Complex(double real, double imaginary) : real_(real), imaginary_(imaginary)
{
}
Complex(const Complex &old)
{
this->real_ = old.real_;
this->imaginary_ = old.imaginary_;
}
Complex &operator=(const Complex &old)
{
this->real_ = old.real_;
this->imaginary_ = old.imaginary_;
return *this;
}
Complex &set_real(double new_val)
{
this->real_ = new_val;
return *this;
}
Complex &set_imaginary(double new_val)
{
this->imaginary_ = new_val;
return *this;
}
friend ostream &operator<<(ostream &os, Complex c);//友元函數的聲明
};
ostream &operator<<(ostream &os, Complex c)
{
return os << '(' << c.real_ << ',' << c.imaginary_ << ')';
}
int main()
{
Complex c(2, 3);
cout << c;
return 0;
}
```
>[test]
>(2,3)
3. **VC++6.0友元函數BUG**
+ 當引入不帶 `.h` 的頭文件 且 使用了命名空間 std 時,將運算符重載函數作為友元函數將無法訪問類的私有成員。
+ 解決方案
1. 使用帶 `.h` 的頭文件,此時不需要使用命名空間 `std`
2. 不要 `using namespace std` ,需要什么標識符就使用什么標識符
> 以下示例可在任意標準的C++中運行
```c++
#include <iostream>
//需要什么標識符就使用什么標識符
using std::ostream;
using std::cout;
class Complex
{
private:
double real_;
double imaginary_;
public:
Complex() : real_(0), imaginary_(0)
{
}
Complex(double real) : real_(real), imaginary_(0)
{
}
Complex(double real, double imaginary) : real_(real), imaginary_(imaginary)
{
}
Complex(const Complex &old)
{
this->real_ = old.real_;
this->imaginary_ = old.imaginary_;
}
Complex &operator=(const Complex &old)
{
this->real_ = old.real_;
this->imaginary_ = old.imaginary_;
return *this;
}
Complex &set_real(double new_val)
{
this->real_ = new_val;
return *this;
}
Complex &set_imaginary(double new_val)
{
this->imaginary_ = new_val;
return *this;
}
friend ostream &operator<<(ostream &os, Complex c);
};
ostream &operator<<(ostream &os, Complex c)
{
return os << '(' << c.real_ << ',' << c.imaginary_ << ')';
}
int main()
{
Complex c(2, 3);
cout << c;
return 0;
}
```
>[test]
>(2,3)
4. **好處** 靈活
5. **弊端** 破壞的類的封裝性
- 閱讀說明
- 1.1 概述
- C++基礎
- 1.2 變量與常量
- 1.2.1 變量
- 1.2.2 字面值常量
- 字符型常量
- 數值型常量
- 1.2.3 cv限定符
- 1.3 作用域
- 1.3.1 標識符
- 1.3.2 *命名空間
- 1.3.3 作用域
- 1.3.4 可見性
- 1.4 數據類型
- 1.4.1 概述
- 1.4.2 處理類型
- 類型別名
- * auto說明符
- * decltype說明符
- 1.4.3 數組
- 1.4.4 指針
- 1.4.5 引用
- 1.5 表達式
- 1.5.1 概述
- 1.5.2 值的類別
- 1.5.3 *初始化
- 1.5.4 運算符
- 算術運算符
- 邏輯和關系運算符
- 賦值運算符
- 遞增遞減運算符
- 成員訪問運算符
- 位運算符
- 其他運算符
- 1.5.5 *常量表達式
- 1.5.6 類型轉換
- 第2章 面向過程編程
- 2.1 流程語句
- 2.1.1 條件語句
- 2.1.2 循環語句
- 2.1.3 跳轉語句
- 2.1.4 *異常處理
- 2.2 函數
- 2.2.1 概述
- 2.2.2 函數參數
- 2.2.3 內置函數
- 2.2.4 函數重載
- 2.2.5 * 匿名函數
- 2.3 存儲和生命期
- 2.3.1 生命周期與存儲區域
- 2.3.2 動態內存
- 2.4 *預處理命令
- 第3章 面向對象編程
- 3.1 概述
- 3.2 類和對象
- 3.3 成員
- 3.3.1 訪問限制
- 3.3.2 常成員
- 3.3.3 靜態成員
- 3.3.4 成員指針
- 3.3.5 this指針
- 3.4 特殊的成員函數
- 3.4.1 概述
- 3.4.2 構造函數
- 3.4.3 析構函數
- 3.4.4 拷貝語義
- 3.4.5 * 移動語義
- 3.5 友元
- 3.6 運算符重載與類型轉換
- 3.6.1 概述
- 3.6.2 重載方法
- 3.6.3 類型轉換
- 3.7 繼承與多態性
- 3.7.1 概述
- 3.7.2 派生類
- 3.7.3 子類型
- 3.7.4 虛基類
- 3.7.5 虛函數
- 3.7.6 抽象類
- 3.8 模板與泛型
- 3.8.1 概述
- 3.8.2 模板類型
- 3.8.3 *模板參數
- 3.8.4 *模板編譯
- 3.8.5 *模板推斷
- 3.8.6 *實例化與特例化
- 第4章 C++標準庫
- 4.1 概述
- 4.2 輸入輸出流
- 4.2.1 概述
- 4.2.2 *流的狀態
- 4.2.3 *常用流
- 4.2.4 *格式化I/O
- 4.2.5 *低級I/O
- 4.2.6 *隨機訪問
- 4.3 *C輸入輸出
- 4.3.1 *字符輸入輸出
- 4.3.2 *格式化輸入輸出
- 4.4 * 容器
- 4.4.1 * 概述
- 4.4.2 * 基本操作
- 4.4.3 * 順序容器
- 4.4.4 * 迭代器
- 4.4.5 * 容器適配器
- 4.5 * 泛型算法
- 4.6 * 內存管理
- 4.6.1 * 自動指針
- 4.7 * 其他設施