通過重載 operator+ 實現 Array a3 = a1 + a2; 的直觀語法。
std::vector。operator+ 參數為 const Array&,返回值為 Array(值返回,利用 RVO/移動語義優化)。#include
using namespace std;
class Array {
static const int SIZE = 6;
int data[SIZE];
public:
Array() { for (int i=0;i
operator+=:避免創建臨時對象,提升性能。operator[]:支持下標訪問 a[i],含邊界檢查。template class Array 支持任意類型、任意大小。運算符重載讓用戶定義類型擁有內置類型般的自然語法,是 C++ 表達能力的核心體現。