1. 程式人生 > >C++ 輸入輸出運算子過載 感想

C++ 輸入輸出運算子過載 感想

在C++中,經常會對輸入輸出運算子進行過載,而在過載的時候用到了友元(Friends)和引用返回(Returning References),這裡對為什麼會這麼用發表一些思考。
比如,下面的類是一個簡單的Student類,其中過載了<<>>

//
// Created by lgl on 17-3-14.
//
#include <iostream>
#include <string>
#include <iostream>
using namespace std;

class Student{
    string name;
    int
age; string country; public: Student(){} Student(string name, int age, string country){ this->name = name; this->age = age; this->country = country; } friend ostream &operator << (ostream &os, const Student &stu){ os << "Name: "
<< stu.name << "; Age: " << stu.age << "; Country: " << stu.country << "."; return os; } friend istream &operator >> (istream &is, Student &stu){ is >> stu.name >> stu.age >> stu.country; return
is; } }; int main() { Student s("xiaoMing", 15, "China"); Student t; cin >> t; cout << s << endl; cout << t << endl; return 0; }

執行:

輸入:
xiaoQiang 78 Earth
輸出:
Name: xiaoMing; Age: 15; Country: China.
Name: xiaoQiang; Age: 78; Country: Earth.

返回引用

先說一下為什麼會返回引用。
當函式返回引用型別時,沒有複製返回值,返回的是物件本身,比如:

const string &shorterString(const string &s1,const string &s2)
{
    return s1.size() < s2.size() ? s1 : s2;
}

這裡引數s1和s2均沒有發生拷貝,直接傳遞了引用,而在返回的時候,也直接返回了較短的字串的引用,整個過程中都沒有發生字串的拷貝,節省了資源,比較高效。
所以對於返回引用,要求必須在函式的引數中,包含有以引用方式或指標方式存在的,需要被返回的引數。如果兩個引數都不是引用,則兩個引數實際上都是放在函式棧裡的區域性變數,返回他們的引用型別會造成不可預知的錯誤(因為函式結束執行時,棧地址被回收了,此時這些地址上的資料可以被其他量覆蓋,再次使用這些地址上的資料,已經不是原來的那個值了。)
比如:

int &smallInt(int a, int b)
{
    return a < b ? a : b;
}

編譯時會發出警告警告:函式可能返回區域性變數的地址 [-Wreturn-local-addr] return a < b ? a : b;。(但是如果形參是string則不會發出警告,原因應該是string屬於字串常量,儲存在只讀資料段,return str只是返回了該字串在只讀資料段所在的首地址,當函式退出後,該字串所在的記憶體不會被回收,所以是正常的。)
關於這些解釋的驗證,詳情見附1。
關於返回值的問題,具體可以參看函式中區域性變數的返回這篇文章,通過幾個例子會對返回值和返回引用有更深的理解。
最後,我們再看ostream &operator <<其實就是在返回ostream的引用,所以形參用的也是ostream的引用ostream & os,而之所以用引用是因為如果不使用引用返回,我們就只能使用值返回,值返回需要複製返回的物件,但是我們無法直接複製一個ostream物件(C++ Primer, 5th Edition, p494),所以不能使用值返回,只能使用引用返回。當然,也可以認為<<操作的結果是一個可以遞進操作的左值,比如(cout << "haha") << "hehe"如果是臨時物件,就不會有遞進操作的能力

友元

那麼為什麼要使用友元呢。因為>><<的前置物件是cincout,不是Student類,所以不能把輸入輸出操作符設計成Student的類成員,而只能設計成普通函式。但是我們又想讓IO運算子讀寫Student類的非公有資料成員,因此自然用到了友元。

通過上面的介紹,我們也就可以記住:過載輸入輸出運算子需要返回引用,因此引數也只能是引用。同時需要將函式定義為友元。理解了這些特徵,就很容易寫出過載函數了。

附1

#include <iostream>

using namespace std;

string &smallString(string a, string b)
{
    return a.size() < b.size() ? a : b;
}

int &smallInt(int a, int b)
{
    return a < b ? a : b;
}

int main()
{
    int &d = smallInt(3, 5);
    string &s = smallString("xixi", "hahaha");
    cout << d << endl;
    cout << s << endl;
    return 0;
}

函式執行結果為:

32744
xixi

再次執行為:

32598
xixi

很明顯int的值在不停變動。如果我們再增添一個函式doNothing,並在呼叫smallInt之後進行呼叫:

#include <iostream>

using namespace std;

string &smallString(string a, string b)
{
    return a.size() < b.size() ? a : b;
}

int &smallInt(int a, int b)
{
    return a < b ? a : b;
}

void doNothing(int a, int b)
{
    a < b ? a : b;
}

int main()
{
    int &d = smallInt(3, 5);
    string &s = smallString("xixi", "hahaha");
    doNothing(44, 55);
    cout << d << endl;
    cout << s << endl;
    return 0;
}

則程式穩定輸出:

44
xixi

即:原來屬於3的地址,現在被填上了44。smallInt函式只返回了3所在的地址,後來在呼叫doNothing的時候,這個地址的內容被44覆蓋了,那麼輸出d的時候,自然就是44。
這很符合之前關於返回引用部分的解釋。