Day6-1 輸入輸出流運算符重載(2025.03.25)
1. 拷貝構造函數的調用時機
2. 友元2.1 友元函數
3. 輸入輸出流運算符重載3.1 關鍵知識點3.2 代碼3.3 關鍵問題3.4 完整代碼
4. 下標訪問運算符 `operator[]`4.1 關鍵知識點4.2 代碼
5. 函數調用運算符 `operator()`5.1 關鍵知識點5.2 代碼5.3 示例5.4 完整代碼
6. 總結
1. 回顧
1.1 拷貝構造函數的調用時機
拷貝構造函數在以下情況會被調用:
-
對象初始化
- 當用一個已經存在的對象去初始化一個剛剛創建的對象時,調用拷貝構造函數。
- 例:
Complex c1(1, 2); Complex c2 = c1; // 調用拷貝構造函數
-
函數參數傳遞
- 當形參與實參都是對象時,在函數調用時會調用拷貝構造函數。
- 例:
void func(Complex c) { } func(c1); // 形參與實參結合時調用拷貝構造函數
-
函數返回對象
- 當函數返回一個對象時,可能調用拷貝構造函數(但現代 C++ 編譯器會嘗試優化此過程,如返回值優化 RVO)。
- 例:
Complex func() {Complex c(3, 4);return c; // 可能調用拷貝構造函數 }
2. 友元
2.1 友元函數
- 友元函數可以訪問類的私有成員。
- 友元聲明可以出現在類的
public
、protected
或private
部分,不影響其權限。 - 友元關系是單向的、不可傳遞的:
- 單向:如果
A
是B
的友元,B
并不會自動成為A
的友元。 - 不可傳遞:如果
A
是B
的友元,B
是C
的友元,A
并不會自動成為C
的友元。
- 單向:如果
3. 輸入輸出流運算符重載
3.1 關鍵知識點
-
operator<<
必須是友元函數- 由于
cout << c1;
左操作數是std::ostream
,不能修改std::ostream
,所以operator<<
不能是Complex
的成員函數。
- 由于
-
operator>>
不能是const
成員函數- 因為
operator>>
需要修改對象的值,因此不能加const
。
- 因為
3.2 代碼
std::ostream& operator<<(std::ostream& os, const Complex& rhs) {if (rhs._imag > 0) {os << rhs._real << " + " << rhs._imag << "i";} else if (rhs._imag == 0) {os << rhs._real;} else {os << rhs._real << " - " << -rhs._imag << "i";}return os;
}std::istream& operator>>(std::istream& is, Complex& rhs) {std::cout << "請輸入復數的實部和虛部:" << std::endl;is >> rhs._real >> rhs._imag;return is;
}
3.3 關鍵問題
-
為什么
operator<<
和operator>>
的返回值是std::ostream&
和std::istream&
?- 這樣可以實現連續輸入輸出:
cout << c1 << c2 << endl; // 連續輸出 cin >> c1 >> c2; // 連續輸入
- 這樣可以實現連續輸入輸出:
-
為什么
operator<<
的ostream&
參數不能去掉&
?- 因為
ostream
的拷貝構造函數已被delete
,不能復制ostream
對象。
- 因為
3.4 完整代碼
#include <iostream>
#include <limits.h>
#include <ostream>using namespace std;//復數
class Complex
{friend Complex operator*(const Complex& lhs, const Complex& rhs);//Complex operator/(const Complex& rhs) const;
public:Complex(double r = 0, double i = 0): _real(r), _imag(i){cout << "Complex(double r = 0, double i = 0)" << endl;}~Complex(){cout << "~Complex()" << endl;}void display() const{if (_imag > 0){cout << _real << " + " << _imag << "i" << endl;}else if (_imag == 0){cout << _real << endl;}else{cout << _real << " - " << -_imag << "i" << endl;}}//成員函數,operator輸出運算符重載 //cout << c1 << endl; //第一個參數cout ,第二個參數c1//std::ostream& operator<<(std::ostream& os, const Complex& rhs); //error!隱含this指針//對于輸出流運算符函數而言,不能寫成成員函數的形式,因為違背了運算符重載的原則,不能改變操作數的順序//std::ostream& operator<<(std::ostream& os);//error!this指針在參數列表的第一個位置/*友元函數可以放在類的 任何位置(public / protected / private),不影響其功能。從代碼規范角度,建議統一放在類定義的開頭或結尾,以提高可讀性。友元關系是單向的,且不具備傳遞性(即類 A 的友元函數不會自動成為類 B 的友元)。*/friend std::ostream& operator<<(std::ostream& os, const Complex& rhs);//輸入流運算符重載friend std::istream& operator>>(std::istream& is, Complex& rhs);private:double _real;double _imag;
};
//問題1 :參數列表中ostrream的引用符號&能不能去掉?
//解答1 :不能去掉因為形參"os"和實參"cout"結合的時候會滿足拷貝構造函數的調用時機,但是ostream中的拷貝構造函數已被delete//問題2 : 函數返回類型中的引用符號&能不能刪除?
//解答2 : 不能取掉,因為return os,返回類型滿足拷貝構造函數的調用時機3//注釋:basic_ifstream( const basic_ifstream& rhs ) = delete; (7) (since C++11)
// basic_ofstream( const basic_ofstream& rhs ) = delete; (7) (since C++11)
//ifstram 和 ofstream 的拷貝構造函數已經從C++11開始刪除了
std::ostream& operator<<(std::ostream& os, const Complex& rhs)
{cout << "std::ostream& operator<<(std::ostream& os, const Complex& rhs)" << endl;if (rhs._imag > 0){os << rhs._real << " + " << rhs._imag << "i" << endl;}else if (rhs._imag == 0){os << rhs._real << endl;}else{os << rhs._real << " - " << -rhs._imag << "i" << endl;}return os;
}void readDouble(std::istream& is, double& rhs)
{while (is >> rhs, !is.eof()){if (is.bad()){std::cerr << "istream is bad" << endl;return;}else if (is.fail()){is.clear();//重置流的狀態is.ignore(std::numeric_limits<::std::streamsize>::max(), '\n');//清空緩沖區cout << "請注意:需要輸入double類型的數據!";}else{cout << "rhs = " << rhs << endl;break;}}
}//輸入流運算符重載
std::istream& operator>>(std::istream& is, Complex& rhs)//因為要修改rhs 的值,所以不能加const
{cout << "std::istream& operator>>(std::istream& is, Complex& rhs)" << endl;cout << "請分別輸入復數的實部和虛部:" << endl;//is >> rhs._real >> rhs._imag;readDouble(is, rhs._real);readDouble(is, rhs._imag);return is;
}void testOutputOperator()
{Complex c1(1, 2);cout << "c1 = ";c1.display();cout << endl << endl;//cout << "c1 = " << c1.display();//為什么沒有做輸出流運算符重載之前上面的寫法 不可行呢? 解答:二元“<<”: 沒有找到接受“void”類型的右操作數的運算符(或沒有可接受的轉換)cout << "c1 = " << c1 << endl;cout << endl;Complex c2;cin >> c2;cout << "c2 = " << c2 << endl;
}int main(int argc, char* argv[])
{testOutputOperator();return 0;
}
4. 下標訪問運算符 operator[]
4.1 關鍵知識點
operator[]
主要用于自定義數組類型,使得obj[idx]
訪問數組元素。- 返回值應為
T&
,以保證能夠修改數組內容。 - 必須進行越界檢查,以防止訪問非法內存。
4.2 代碼
char& CharArrar::operator[](size_t idx) {if (idx < _size) {return _data[idx];} else {static char charNull = '\0';return charNull;}
}
5. 函數調用運算符 operator()
5.1 關鍵知識點
- 使對象像函數一樣調用,稱為仿函數(函數對象)。
- 可存儲狀態,例如調用次數。
5.2 代碼
class FunctionObject {
public:int operator()(int x, int y) {++_cnt;return x + y;}int operator()(int x, int y, int z) {++_cnt;return x * y * z;}private:int _cnt = 0;
};
5.3 示例
FunctionObject fo;
cout << "fo(3, 4) = " << fo(3, 4) << endl;
cout << "fo(3, 4, 5) = " << fo(3, 4, 5) << endl;
5.4 完整代碼
//bracket.h
#pragma once
#include <iostream>
#include <string.h>using namespace std;
class CharArrar
{
public:CharArrar(size_t sz = 10):_size(sz), _data(new char[_size]){cout << " CharArrar(size_t sz = 10)" << endl;}~CharArrar(){cout << "~CharArrar()" << endl;if (_data){delete _data;_data = nullptr;}}size_t size() const{return _size;}//下標訪問運算符的重載//int arr[10] = { 1,2,3,4,5 };//arr.operator[](idx);char& operator[](size_t idx);
private:size_t _size;char* _data;
};
//bracket.cpp
#include "bracket.h"/*
要修復錯誤 C2106: “=”: 左操作數必須為左值,需要確保 CharArrar 類的
下標運算符 operator[] 返回一個可修改的左值。當前的 operator[] 聲明
返回的是一個 char,這不是一個可修改的左值。我們需要將其修改為返回一個 char&。
*/
char& CharArrar::operator[](size_t idx)
{if (idx < _size){return _data[idx];}else{static char charNUll = '\0';//靜態變量延長生命周期return charNUll; //使得返回的實體生命周期比函數的生命周期長}
}//C++中優勢:重載的下標訪問運算符考慮了越界的問題
//引用什么時候需要加上?
//1.如果返回類型是類型的時候,可以減少拷貝構造函數的執行
//2.有可能需要一個左值(來調用右操作數),而不是拷貝后的右值、
//3.cout << "111" << c1 << endl << 10 << 1 << endl,像這種情況下連續使用的時候可以加上引用
//parenthese.h
#pragma once
#include <iostream>using namespace std;class FunctionObject
{
public:int operator()(int x, int y);int operator()(int x, int y, int z);private:int _cnt;//記錄被調用的次數(函數對象狀態)
};
parenthese.cpp
#include <iostream>
#include "parenthese.h"using namespace std;int FunctionObject::operator()(int x, int y)
{++_cnt;cout << "int operator()(int x, int y)" << endl;return x + y;
}int FunctionObject::operator()(int x, int y, int z)
{cout << "int operator()(int x, int y,int z)" << endl;++_cnt;return x * y * z;
}
//main.cpp
#include <iostream>
#include "bracket.h"
#include "parenthese.h"using namespace std;int add(int x, int y)
{cout << "int add(int x,int y)" << endl;static int cnt = 0;++cnt;return x + y;
}void testFunctionObject()
{FunctionObject fo;int a = 3;int b = 4;int c = 5;//fo本質上是一個對象,但是他的使用cout << "fo(a,b) = " << fo(a, b) << endl;cout << "fo(a, b ,c) = " << fo(a, b, c) << endl;cout << endl;//正經的函數cout << "add(a, b) = " << add(a, b) << endl;
}void testCharArrar()
{//把字符串中的內容拷貝到CharArrayconst char* pstr = "hello cpp";CharArrar ca(strlen(pstr) + 1);for(size_t idx = 0; idx != ca.size(); ++idx){//ca[idx] = pstr[idx];//上面和下面兩條代碼等價ca.operator[](idx) = pstr[idx];}for (size_t idx = 0; idx != ca.size(); ++idx){cout << ca[idx] << " ";}cout << endl;
}int main(int argc, char** argv)
{testFunctionObject();testCharArrar();return 0;
}