這個問題在這里已經有了答案: 不能多載 operator<< 作為成員函式 3 個答案 13 小時前關閉。
從 c 如何編程 deitel 第 10 版
//Fig. 10.3: PhoneNumber.h
//PhoneNumber class definition
#ifndef PHONENUMBER_H
#define PHONENUMBER_H
#include <iostream>
#include <string>
class PhoneNumber
{
friend std::ostream& operator<<(std::ostream&, const PhoneNumber&);
friend std::istream& operator>>(std::istream&, PhoneNumber&);
private:
std::string areaCode; //3-digit area code
std::string exchange; //3-digit exchange
std::string line; //4-digit line
};
#endif // PHONENUMBER_H
//Fig. 10.4: PhoneNumber.cpp
//Overloaded stream insertion and stream extraction operators
#include <iomanip>
#include "PhoneNumber.h"
using namespace std;
//overloaded stream insertion operator; cannot be a member function
//if we would like to invoke it with cout << somePhoneNumber;
ostream& operator<<(ostream& output, const PhoneNumber& number)
{
output << "Area code: " << number.areaCode << "\nExchange: "
<< number.exchange << "\nLine: " << number.line << "\n"
<< "(" << number.areaCode << ") " << number.exchange << "-"
<< number.line << "\n";
return output; //enables cout << a << b << c;
}
//overloaded stream extraction operator; cannot be a member function
//if we would like to invoke it with cin >> somePhoneNumber;
istream& operator>>(istream& input, PhoneNumber& number)
{
input.ignore(); //skip (
input >> setw(3) >> number.areaCode; //input area code
input.ignore(2); //skip ) and space
input >> setw(3) >> number.exchange; //input exchange
input.ignore(); //skip dash (-)
input >> setw(4) >> number.line; //input line
return input; //enables cin >> a >> b >> c
}
//Fig. 10.5: fig10_5.cpp
//Demonstrating Class PhoneNumber's overloaded stream insertion
//and stream extraction operators.
#include <iostream>
#include "PhoneNumber.h"
#include <string>
using namespace std;
int main()
{
PhoneNumber phone; //create object phone
cout << "Enter phone number in the form (555) 555-5555:" << endl;
//cin >> phone invokes operator>> by implicitly issuing
//the non-member function call operator>>(cin, phone)
cin >> phone;
//cout << phone invokes operator<< bby implicitly issuing
//the non-member function call operator<<(cout, phone)
cout << phone << endl;
}
我從書中了解到,二元運算子的多載運算子函式只有當左運算元是該函式所屬的類的物件時才能成為成員函式,因為成員函式只能由作為物件的左運算元訪問類的。
但是我不明白這部分
" 多載流插入運算子 (<<) 用于左運算元為 ostream& 型別的運算式,如 cout << classObject。以這種方式使用運算子,其中右運算元是用戶定義的物件類,它必須作為非成員函式多載。要成為成員函式,運算子 << 必須是類 ostream 的成員。這對于用戶定義的類是不可能的,因為我們不允許修改 C 標準庫類。 "
我的問題是
- 流提取和插入運算子是否分別是 ostream 和 istream 的一部分?
- 以粗體突出顯示的部分是什么意思?
我的猜測是我們不能將用戶定義的函式添加到 C 標準庫類中,但是粗體部分讓我有點困惑。我們是否必須將運算子 << 添加為類 ostream 的成員才能使多載的運算子函式成為 ostream 的成員函式?
我一直認為流 << 和 >> 運算子分別是 ostream 和 istream 的一部分。
如果我錯了,請糾正我。先感謝您。
uj5u.com熱心網友回復:
- 不,不適用于常規的用戶定義型別。您
friend在類定義中的 s 函式是自由函式:std::ostream& operator<<(std::ostream&, const PhoneNumber&); std::istream& operator>>(std::istream&, PhoneNumber&);std::basic_ostream確實有一些operator<<基本型別的成員多載,例如int, 和標準庫中定義的一些型別。如果您從其中一些標準型別(如 )繼承std::basic_streambuf<CharT, Traits>,您將能夠basic_ostream::operator<<為這些型別使用成員多載。 - 這意味著您不允許修改
std::basic_ostream添加std::basic_ostream& operator<<(const PhoneNumber&)為成員函式的定義。
uj5u.com熱心網友回復:
您可能已經閱讀過有關多載的內容。沒有一個函式被呼叫operator<<,有多個。其中一些是班級成員,另一些則不是。編譯器將operator<<根據這兩個引數選擇正確的。
當編譯器查找 的多載時,它只會operator<<檢查左側物件的類方法。(如果那甚至是一個類。在引數中是整數)。出于這個原因,當編譯器看到它不會尋找.1<<3==8std::cout << YourClass{}YourClass::operator<<(ostream&)
如果你寫YourClass{} << std::cout(非常不尋常),編譯器會查找YourClass. 規則就是規則,并且沒有禁止該命令的硬性規則。但它會破壞通常的鏈接std::cout << a << b << c,所以非常不方便。所以為了使通常的鏈接作業,我們按照你的教科書告訴你的那樣做。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/493791.html
上一篇:`std::cout`和`std::ostream`之間有什么關系?
下一篇:模板類引數作為型別
