一、下標(biāo) [] 運算符重載
1、數(shù)組類回顧
在之前的博客 【C++】面向?qū)ο笫纠?- 數(shù)組類 ( 示例需求 | 創(chuàng)建封裝類 | 數(shù)組類頭文件 Array.h | 數(shù)組類實現(xiàn) Array.cpp | 測試類 Test.cpp - 主函數(shù)入口 ) 中 , 實現(xiàn)了一個數(shù)組類 , 在一個類中 , 封裝了一個 int 類型的指針 , 該指針指向堆內(nèi)存的 內(nèi)存空間 , 用于存放一個數(shù)組 ;
核心是 2 2 2 個成員變量 , 記錄指向堆內(nèi)存的指針 , 和 數(shù)組長度 ;
private:
// 數(shù)組長度
int m_length;
// 指向數(shù)組數(shù)據(jù)內(nèi)存 的指針
int* m_space;
數(shù)組都可以使用下標(biāo)進(jìn)行訪問 , 如果要使用下標(biāo)訪問 自定義 Array 類對象 , 需要重載 [] 下標(biāo)運算符 ;
下面基于 博客 【C++】面向?qū)ο笫纠?- 數(shù)組類 ( 示例需求 | 創(chuàng)建封裝類 | 數(shù)組類頭文件 Array.h | 數(shù)組類實現(xiàn) Array.cpp | 測試類 Test.cpp - 主函數(shù)入口 ) 中 的代碼 , 重載 數(shù)組下標(biāo) [] 操作符 ;
2、下標(biāo) [] 運算符重載
使用 成員函數(shù) 實現(xiàn) 下標(biāo) [] 運算符重載 :
-
首先 , 寫出函數(shù)名 , 函數(shù)名規(guī)則為 " operate " 后面跟上要重載的運算符 ,
- 要對 Array a 對象 , 使用 [] 運算符 , 使用時用法為 a[i] ;
- 函數(shù)名是
operate[]
;
operate[]
-
然后 , 根據(jù)操作數(shù) 寫出函數(shù)參數(shù) , 參數(shù)一般都是 對象的引用 ;
- 要對 Array a 對象 , 使用 [] 運算符 , 使用時用法為 a[i] ,
- 左操作數(shù) : 其中 左操作數(shù) 是 a ,
- 右操作數(shù) : 運算符里面還有一個 int 類型的索引值 , 是右操作數(shù) ;
- 如果是成員函數(shù) , 則將重載函數(shù)寫在 左操作數(shù) 中 , 在 重載操作數(shù)的 成員函數(shù)中 this 指針就是 左操作數(shù) ;
- 此時只需要額外指定一個 int 類型右操作數(shù) 即可 ;
operator[](int i)
-
再后 , 根據(jù)業(yè)務(wù)完善返回值 , 返回值可以是 引用 / 指針 / 元素 ;
- 要對 Array a 對象 , 使用 [] 運算符 , 使用時用法為 a[i] ;
- a[i] 除了獲取值之外 , 還要可以設(shè)置值 ;
- a[i] 既可以作為左值 , 又要可以作為右值使用 ;
- 因此返回值必須是 int 值本身 , 如果返回 int 值就只是一個副本值 沒有意義 , 這里返回 int& 引用類型 ;
int& operator[](int i)
- 最后 , 實現(xiàn)函數(shù)體 , 編寫具體的運算符操作業(yè)務(wù)邏輯 ;
// 數(shù)組下標(biāo) [] 操作符重載
int& Array::operator[](int i)
{
return m_space[i];
}
在測試類中 , 使用數(shù)組下標(biāo)為數(shù)組設(shè)置值 , 并使用數(shù)組下標(biāo)操作符獲取數(shù)組元素的值 ;
Array array(3);
// 設(shè)置 array 數(shù)組值
for (int i = 0; i < array.length(); i++)
{
//array.setData(i, i + 5);
array[i] = i + 5;
}
// 打印 array 數(shù)組值
for (int i = 0; i < array.length(); i++)
{
//cout << array.getData(i) << endl;
cout << array[i] << endl;
}
二、完整代碼示例
1、Array.h 數(shù)組頭文件
#pragma once
#include "iostream"
using namespace std;
class Array
{
public:
// 無參構(gòu)造函數(shù)
Array();
// 有參構(gòu)造函數(shù)
Array(int len);
// 拷貝構(gòu)造函數(shù)
Array(const Array& array);
// 析構(gòu)函數(shù)
~Array();
public:
// 設(shè)置數(shù)組數(shù)據(jù)
void setData(int index, int value);
// 獲取數(shù)組數(shù)據(jù)
int getData(int index);
// 獲取數(shù)組長度
int length();
public:
// 數(shù)組下標(biāo) [] 操作符重載
int& operator[](int i);
private:
// 數(shù)組長度
int m_length;
// 指向數(shù)組數(shù)據(jù)內(nèi)存 的指針
int* m_space;
};
2、Array.cpp 數(shù)組實現(xiàn)類
#include "Array.h"
// 無參構(gòu)造函數(shù)
Array::Array()
{
// 設(shè)置數(shù)組長度
m_length = 10;
// 為數(shù)組在堆內(nèi)存中分配內(nèi)存
m_space = new int[m_length];
cout<< " 調(diào)用無參構(gòu)造函數(shù) " << endl;
}
// 有參構(gòu)造函數(shù)
Array::Array(int len)
{
// 設(shè)置數(shù)組長度
m_length = len;
// 為數(shù)組在堆內(nèi)存中分配內(nèi)存
m_space = new int[m_length];
cout << " 調(diào)用有參構(gòu)造函數(shù) " << endl;
}
// 拷貝構(gòu)造函數(shù)
// 這是一個深拷貝 拷貝構(gòu)造函數(shù)
Array::Array(const Array& array)
{
// 設(shè)置數(shù)組長度
m_length = array.m_length;
// 創(chuàng)建數(shù)組
m_space = new int[m_length];
// 為數(shù)組賦值
for (int i = 0; i < m_length; i++)
{
m_space[i] = array.m_space[i];
}
cout << " 調(diào)用拷貝構(gòu)造函數(shù) " << endl;
}
// 析構(gòu)函數(shù)
Array::~Array()
{
if (m_space != NULL)
{
// 釋放 new int[m_length] 分配的內(nèi)存
delete[] m_space;
m_space = NULL;
}
cout << " 調(diào)用析構(gòu)函數(shù) " << endl;
}
// 設(shè)置數(shù)組數(shù)據(jù)
void Array::setData(int index, int value)
{
m_space[index] = value;
}
// 獲取數(shù)組數(shù)據(jù)
int Array::getData(int index)
{
return m_space[index];
}
// 獲取數(shù)組長度
int Array::length()
{
return m_length;
}
// 數(shù)組下標(biāo) [] 操作符重載
int& Array::operator[](int i)
{
return m_space[i];
}
3、Test.cpp 測試類
#include "iostream"
using namespace std;
#include "Array.h"
int main() {
Array array(3);
// 設(shè)置 array 數(shù)組值
for (int i = 0; i < array.length(); i++)
{
//array.setData(i, i + 5);
array[i] = i + 5;
}
// 打印 array 數(shù)組值
for (int i = 0; i < array.length(); i++)
{
//cout << array.getData(i) << endl;
cout << array[i] << endl;
}
// 使用拷貝構(gòu)造函數(shù) 賦值
Array array2 = array;
// 打印 array2 數(shù)組值
for (int i = 0; i < array2.length(); i++)
{
//cout << array2.getData(i) << endl;
cout << array2[i] << endl;
}
// 控制臺暫停 , 按任意鍵繼續(xù)向后執(zhí)行
system("pause");
return 0;
}
4、執(zhí)行結(jié)果
執(zhí)行結(jié)果 :文章來源:http://www.zghlxwxcb.cn/news/detail-728094.html
調(diào)用有參構(gòu)造函數(shù)
5
6
7
調(diào)用拷貝構(gòu)造函數(shù)
5
6
7
請按任意鍵繼續(xù). . .
文章來源地址http://www.zghlxwxcb.cn/news/detail-728094.html
到了這里,關(guān)于【C++】運算符重載 ⑩ ( 下標(biāo) [] 運算符重載 | 函數(shù)原型 int& operator[](int i) | 完整代碼示例 )的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!