C++輸入輸出操作符重載的深入分析
更新時間:2013年05月29日 11:39:20 作者:
本篇文章是對C++輸入輸出操作符重載進行了詳細的分析介紹,需要的朋友參考下
操作符的重載有一些規(guī)則:
1. 重載操作符必須具有一個類類型或枚舉類型操作數(shù)。這條規(guī)則強制重載操作符不能重新定義用于內置類型對象的操作符的含義。
如:int operator+(int, int), 不可以
2. 為類設計重載操作符的時候,必須選擇是將操作符設置為類成員還是普通非成員函數(shù)。在某些情況下,程序沒有選擇,操作符必須是成員;在另外一些情況下,有些經(jīng)驗可以指導我們做出決定。下面是一些指導:
a. 賦值(=),下標([]),調用(())和成員訪問箭頭(->)等操作符必須定義為成員,將這些操作符定義為非成員函數(shù)將在編譯時標記為錯誤。
b. 像賦值一樣,復合賦值操作符通常應定義為類的成員。與賦值不同的是,不一定非得這樣做,如果定義為非成員復合賦值操作符,不會出現(xiàn)編譯錯誤。
c. 改變對象狀態(tài)或與給定類型緊密聯(lián)系的其他一些操作符,如自增,自減和解引用,通常應定義為類成員。
d 對稱的操作符,如算術操作符,相等操作符,關系操作符和位操作符,最好定義為普通非成員函數(shù)。
e io操作符必須定義為非成員函數(shù),重載為類的友元。
// OverloadCinCout.cpp : 定義控制臺應用程序的入口點。
//
#include "stdafx.h"
#include "iostream"
#include "string"
using namespace std;
class Fruit
{
public:
Fruit(const string &nst = "apple", const string &cst = "green"):name(nst),colour(cst){}
~Fruit(){}
friend ostream& operator << (ostream& os, const Fruit& f); //輸入輸出流重載,不是類的成員,
friend istream& operator >> (istream& is, Fruit& f); // 所以應該聲明為類的友元函數(shù)
private:
string name;
string colour;
};
ostream& operator << (ostream& os, const Fruit& f)
{
os << "The name is " << f.name << ". The colour is " << f.colour << endl;
return os;
}
istream& operator >> (istream& is, Fruit& f)
{
is >> f.name >> f.colour;
if (!is)
{
cerr << "Wrong input!" << endl;
}
return is;
}
int _tmain(int argc, _TCHAR* argv[])
{
Fruit apple;
cout << "Input the name and colour of a kind of fruit." << endl;
cin >> apple;
cout << apple;
return 0;
}
1. 重載操作符必須具有一個類類型或枚舉類型操作數(shù)。這條規(guī)則強制重載操作符不能重新定義用于內置類型對象的操作符的含義。
如:int operator+(int, int), 不可以
2. 為類設計重載操作符的時候,必須選擇是將操作符設置為類成員還是普通非成員函數(shù)。在某些情況下,程序沒有選擇,操作符必須是成員;在另外一些情況下,有些經(jīng)驗可以指導我們做出決定。下面是一些指導:
a. 賦值(=),下標([]),調用(())和成員訪問箭頭(->)等操作符必須定義為成員,將這些操作符定義為非成員函數(shù)將在編譯時標記為錯誤。
b. 像賦值一樣,復合賦值操作符通常應定義為類的成員。與賦值不同的是,不一定非得這樣做,如果定義為非成員復合賦值操作符,不會出現(xiàn)編譯錯誤。
c. 改變對象狀態(tài)或與給定類型緊密聯(lián)系的其他一些操作符,如自增,自減和解引用,通常應定義為類成員。
d 對稱的操作符,如算術操作符,相等操作符,關系操作符和位操作符,最好定義為普通非成員函數(shù)。
e io操作符必須定義為非成員函數(shù),重載為類的友元。
復制代碼 代碼如下:
// OverloadCinCout.cpp : 定義控制臺應用程序的入口點。
//
#include "stdafx.h"
#include "iostream"
#include "string"
using namespace std;
class Fruit
{
public:
Fruit(const string &nst = "apple", const string &cst = "green"):name(nst),colour(cst){}
~Fruit(){}
friend ostream& operator << (ostream& os, const Fruit& f); //輸入輸出流重載,不是類的成員,
friend istream& operator >> (istream& is, Fruit& f); // 所以應該聲明為類的友元函數(shù)
private:
string name;
string colour;
};
ostream& operator << (ostream& os, const Fruit& f)
{
os << "The name is " << f.name << ". The colour is " << f.colour << endl;
return os;
}
istream& operator >> (istream& is, Fruit& f)
{
is >> f.name >> f.colour;
if (!is)
{
cerr << "Wrong input!" << endl;
}
return is;
}
int _tmain(int argc, _TCHAR* argv[])
{
Fruit apple;
cout << "Input the name and colour of a kind of fruit." << endl;
cin >> apple;
cout << apple;
return 0;
}
相關文章
輸入一個字符串,取出其中的整數(shù)(實現(xiàn)代碼)
輸入一個字符串,內含所有數(shù)字和非數(shù)字字符。將其中連續(xù)的數(shù)字作為一個整數(shù),依次存放到一個數(shù)組中,統(tǒng)計共有多少個整數(shù),并輸出這些數(shù)2013-09-09
C/C++ Qt 基本文件讀寫的基本使用(2種實現(xiàn))
文件的讀寫是很多應用程序具有的功能,本文主要介紹了兩種實現(xiàn)方法,第一種使用QFile類的IODevice讀寫功能直接讀寫,第二種是利用 QFile和QTextStream結合起來,用流的方式進行文件讀寫2021-11-11

