運算符重載的主要目的是爲了讓類對象能像普通數據類型一樣能夠進行加減乘除,自加自減等操作,非常直觀方便。現在來回顧C++的自加減(分前置與後置)以及不等號非運算符,賦值運算符的重載。
1 ++重載
(1)前置++運算符的重載方式:
成員函數的重載: 函數類型& operator++()
友元函數的重載:friend 函數類型& operator++(類類型& )
(2)後置++運算符的重載方式:
成員函數的重載:函數類型& operator++(int)
友元函數的重載:friend 函數類型& operator++(類類型&, int)
注意,爲了區分前置++與後置++的區別,需要在參數後增加一個"int"以示區分。含有"int"的重載方式爲後置++,否則爲前置++。前置--與後置--類似用法。前面說過,成員函數與友元函數的重載如果同時存在時,會先調用成員函數的重載,但是在++或--時,成員函數與友元函數的重載是不能同時存在的。
下面舉一個例子:
- #ifndef _INTEGER_H_
- #define _INTEGER_H_
- class Integer
- {
- public:
- Integer(int n);
- ~Integer();
- void Display();
- Integer& operator++(); //成員方式重載前置++
- Integer& operator++(int); //成員方式重載後置++
- friend Integer& operator++(Integer& i);//友元函數重載前置++
- friend Integer& operator++(Integer& i, int);//友元函數重載後置++
- private:
- int n_;
- };
- #endif
下面是它們的具體代碼實現:
- #include "Integer.h"
- Integer::Integer(int n):n_(n){}
- Integer::~Integer(){}
- void Integer::Display() const
- {
- cout << n_ << endl;
- }
- //最好優先使用成員函數重載,
- //成員函數重載前置++
- Integer& Integer::operator++()
- {
- ++n_;
- return *this;
- }
- //成員函數重載後置++
- Integer& Integer::operator++(int)
- {
- Integer tmp(n_);
- ++n_;
- return tmp;
- }
- //友元重載前置++
- Integer& operator++(Integer& i)
- {
- ++i.n_;
- return i;
- }
- //友元重載後置++
- Integer& operator++(Integer& i, int)
- {
- Integer tmp(i.n_);
- ++i.n_;
- return tmp;
- }
關於!及=賦值運算符的重載以String類進行說明:
下面是String類的定義:
- #ifndef STRING_H_
- #define STRING_H_
- class String
- {
- public:
- explicit String(const char *str="");
- String(const String& other);
- ~String();
- //應用於s="abc";的情況
- String& operator=(const char *str);//重載=
- bool operator!() const; //重載!
- void Display() const;
- private:
- char* str_;
- char* AllocAndCopy(const char* str);
- };
- #endif
下面是具體實現:
- #include <string.h>
- #include <iostream>
- #include "String.h"
- using namespace std;
- String::String(const char *str)
- {
- str_ = AllocAndCopy(str);
- }
- String::~String()
- {
- delete [] str_;
- }
- char* String::AllocAndCopy(const char* str)
- {
- int len = strlen(str)+1;
- char* newstr = new char[len];
- memset(newstr, 0, len);
- strcpy(newstr, str);
- return newstr;
- }
- //深拷貝,copy assign
- String& String::operator=(const String& other) //s1 = s2
- {
- if( this == &other)
- return *this;
- delete [] str_;
- str_ = AllocAndCopy(other.str_);
- return *this;
- }
- String& String::operator=(const char* str) //s1="abc"
- {
- delete [] str_;
- str_ = AllocAndCopy(str);
- return *this;
- }
- bool String::operator!() const
- {
- return (strlen(str_) != 0 )
- }
- void String::Display() const
- {
- cout << str_ << endl;
- }
針對String類寫的一個簡單的用例:
- #include "String.h"
- #include <iostream>
- using namespace std;
- int main()
- {
- String s1("abc");
- String s2(s1);//copy.
- String s3;//帶默認參數
- s3 = s1; //調用賦值運算符
- s3.Display();
- //默認的處理方式爲:將字符串構造成String類對象,再賦值至s3,如果在構造函數前加上explicit聲明,則會發生編譯錯誤。解決的方式需要重載一個參數爲const char *的等號運算符即可
- s3 = "xxx"; //調用參數爲const char *的賦值運算符
- String s4;
- bool notempty;
- notempty = !s4;
- cout << notempty << endl; //1
- s4 = "aaa";
- notempty = !s4;
- cout << notempty << endl; //0
- return 0;
- }