目錄
- 1. 陣列運算子多載
- 陣列運算子多載
- 陣列類IntArray改進
- 2. 函式運算子多載(函式物件)
- 3. 指標運算子多載與智能指標
- 指標運算子多載
- 智能指標
- 4. 前置、后置運算子多載
- 多載實作
- 前置、后置多載的區別
1. 陣列運算子多載
陣列運算子多載
通過多載陣列運算子,可以使類的物件支持陣列的下標訪問
- 陣列運算子只能多載為類的成員函式
- 多載函式能且僅能使用一個引數,也就是陣列下標
- 可以定義不同引數的多個多載函式
在多載陣列運算子時,要記得陣列運算子的原生語意——陣列訪問和指標運算,
a[n] <--> *(a + n) <--> *(n + a) <--> n[a]
#include <iostream>
#include <string>
using namespace std;
class Test
{
int a[3];
public:
int &operator [] (int i)
{
return a[i];
}
int &operator [] (const string &s)
{
if( s == "1st" )
{
return a[0];
}
else if( s == "2nd" )
{
return a[1];
}
else if( s == "3rd" )
{
return a[2];
}
return a[0];
}
};
int main()
{
Test t;
for (int i = 0; i < 3; i++)
{
t[i] = i;
}
for (int i = 0; i < 3; i++)
{
cout << t[i] << endl;
}
cout << endl;
cout << t["3rd"] << endl;
cout << t["2nd"] << endl;
cout << t["1st"] << endl;
return 0;
}

陣列類IntArray改進
IntArray.h
class IntArray
{
public:
int &operator [] (int index); //Add
IntArray &self(); //Add
};
IntArray.cpp
int &IntArray::operator [] (int index)
{
return m_pointer[index];
}
IntArray &IntArray::self()
{
return *this;
}
2. 函式運算子多載(函式物件)
- 函式運算子只能通過類的成員函式多載
- 可以定義不同引數的多個多載函式
- 函式運算子多載的本質是使用具體的類物件取代函式,也就是函式物件,函式物件具備函式呼叫的行為
- 函式物件用于在工程中取代函式指標
/*
* 本示例代碼實作以下需求:
* - 撰寫一個函式,可以獲得斐波那契數列每項的值
* - 每呼叫一次回傳一個值
* - 函式可根據需要重復使用
*/
#include <iostream>
#include <string>
using namespace std;
class Fib
{
int a0;
int a1;
public:
Fib()
{
a0 = 0;
a1 = 1;
}
Fib(int n)
{
a0 = 0;
a1 = 1;
for(int i = 2; i <= n; i++)
{
int t = a1;
a1 = a0 + a1;
a0 = t;
}
}
int operator () ()
{
int ret = a1;
a1 = a0 + a1;
a0 = ret;
return ret;
}
};
int main()
{
Fib fib;
for(int i = 0; i < 10; i++)
{
cout << fib() << endl;
}
cout << endl;
for(int i = 0; i < 5; i++)
{
cout << fib() << endl;
}
cout << endl;
Fib fib2(10);
for(int i = 0; i < 5; i++)
{
cout << fib2() << endl;
}
return 0;
}

3. 指標運算子多載與智能指標
指標運算子多載
指標運算子指的是->和*
- 指標運算子只能通過類的成員函式多載
- 多載函式不能使用引數,也就是說,只能定義一個多載函式
智能指標
- 利用指標運算子多載,可以實作智能指標
- 智能指標只能用來指向堆空間中的物件或者變數!!!
- 智能指標在生命周期結束時會自動釋放堆空間
- 智能指標的意義在于減少開發人員的記憶體管理作業,最大程度上避免記憶體問題,
/*
* 實作智能指標類Pointer,要求如下:
* - 一片堆空間最多只能由一個指標標識
* - 禁止指標運算和指標比較
*/
#include <iostream>
#include <string>
using namespace std;
class Test
{
int i;
public:
Test(int i)
{
cout << "Test(int i)" << endl;
this->i = i;
}
int value()
{
return i;
}
~Test()
{
cout << "~Test()" << endl;
}
};
class Pointer
{
private:
Test *mp;
public:
Pointer(Test *p = NULL)
{
mp = p;
}
Pointer(const Pointer &obj)
{
mp = obj.mp;
const_cast<Pointer &>(obj).mp = NULL;
}
Pointer &operator = (const Pointer &obj)
{
if (this != &obj)
{
delete mp;
mp = obj.mp;
const_cast<Pointer &>(obj).mp = NULL;
}
return *this;
}
Test *operator -> ()
{
return mp;
}
Test &operator * ()
{
return *mp;
}
bool isNull()
{
return (mp == NULL);
}
~Pointer()
{
delete mp;
}
};
int main()
{
Pointer p1 = new Test(0);
Pointer p2 = p1;
cout << p1.isNull() << endl;
cout << p2->value() << endl;
Pointer p3;
p3 = p2;
cout << p2.isNull() << endl;
cout << p3->value() << endl;
return 0;
}

4. 前置、后置運算子多載
多載實作
前置、后置運算子指的是++和--,我們以++為例進行講解,--和++是一樣的
- 全域函式和成員函式均可進行多載
- 多載前置++運算子不需要引數
- 多載后置++運算子需要一個int型別的占位引數
#include <iostream>
#include <string>
using namespace std;
class Test
{
int mValue;
public:
Test(int i)
{
mValue = https://www.cnblogs.com/songhe364826110/p/i;
}
int value()
{
return mValue;
}
Test &operator ++ ()
{
++mValue;
return *this;
}
Test operator ++ (int)
{
Test ret(mValue);
mValue++;
return ret;
}
};
int main()
{
Test t1(0);
Test t2(0);
Test t = t1++;
cout << t.value() << endl;
cout << t1.value() << endl;
t = ++t2;
cout << t.value() << endl;
cout << t2.value() << endl;
return 0;
}

前置、后置多載的區別
- 對于基礎型別的變數,前置++和后置++的效率基本相同,沒有什么區別
- 對于型別別的物件,前置++的效率高于后置++,因為后置++多載會呼叫構造與解構式
- 在工程中盡量使用前置++多載以提高程式效率
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/103667.html
標籤:C++
上一篇:C++類的this指標詳解
下一篇:C++—多型與繼承
