拷貝構造函數聲明為 `CExample(CExample ex)` (值傳遞) 而非 `CExample(const CExample& ex)` (引用傳遞),創建對象時程序**無限遞歸直到棧溢出**。
`CExample(const CExample& ex)`:
// 錯誤:值傳遞 → 無限遞歸
class Bad {
public:
Bad(Bad other) { } // 形參按值,調用此構造函數需先構造 other → 死循環
};
// 正確:const 引用傳遞 → 無遞歸
class Good {
public:
Good(const Good& other) { } // 形參是引用,綁定實參,無構造開銷
};
Good& operator=(const Good& other) { // 必須是引用
if (this != &other) { ... }
return *this;
}
// 若寫 Good operator=(Good other) 同樣會無限遞歸
**C++ 規定拷貝構造函數、移動構造函數、賦值運算符的參數必須是引用類型** (通常加 `const`)。這不是風格建議,而是**語言機制強制要求**,否則觸發無限遞歸的編譯時/運行時錯誤。