# 複製建構函式、物件的指定

SomeClass s1;
SomeClass s2 = s1;

``#include <iostream>using namespace std;class Employee { public:     Employee() {        _num = 0;         _years = 0.0;    }      Employee(int num, double years) {         _num = num;         _years = years;     }     int num() {        return _num;    }     double years() {        return _years;    } private:    int _num;    double _years; }; int main() {    Employee p1(101, 3.5);     Employee p2 = p1;     cout << "p1 addr:\t" << &p1 << endl;    cout << "p1.num: \t" << p1.num() << endl;    cout << "p1.years:\t" << p1.years() << endl;     cout << "p2 addr:\t" << &p2 << endl;     cout << "p2.num: \t" << p2.num() << endl;    cout << "p2.years:\t" << p2.years() << endl;     return 0;}``

p1 addr:        0x22ff60
p1.num:         101
p1.years:       3.5
p2 addr:        0x22ff50
p2.num:         101
p2.years:       3.5

SafeArray arr1(10);
SafeArray arr2 = arr1;

SafeArray::~SafeArray() {
delete [] _array;
}

arr2複製了arr1的屬性，當然也包括了_array指標，如果arr1資源先被回收了，但arr2的_array仍然參考至一個已被回收資源的位 址，這時再存取該位址的資料就有危險，例如下面這段程式就可能造成程式不可預期的結果：
SafeArray *arr1 = new SafeArray(10);
SafeArray arr2 = *arr1;
delete arr1;

• SafeArray.h
``class SafeArray { public:     int length;     // 複製建構函式     SafeArray(const SafeArray&);    // 建構函式     SafeArray(int);     // 解構函式     ~SafeArray();     int get(int);     void set(int, int); private:    int *_array;     bool isSafe(int i); };``

• SafeArray.cpp
``#include "SafeArray.h"// 複製建構函式 SafeArray::SafeArray(const SafeArray &safeArray)                                  : length(safeArray.length) {    _array = new int[safeArray.length];     for(int i = 0; i < safeArray.length; i++) {        _array[i] = safeArray._array[i];    }}// 動態配置陣列SafeArray::SafeArray(int len) {    length = len;    _array = new int[length];}// 測試是否超出陣列長度bool SafeArray::isSafe(int i) {    if(i > length || i < 0) {        return false;    }     else {        return true;    }}// 取得陣列元素值int SafeArray::get(int i) {    if(isSafe(i)) {        return _array[i];    }     return 0;}// 設定陣列元素值void SafeArray::set(int i, int value) {    if(isSafe(i)) {        _array[i] = value;    }}// 刪除動態配置的資源SafeArray::~SafeArray() {    delete [] _array;}``

• SafeArray.h
``class SafeArray { public:     int length;      // 複製建構函式     SafeArray(const SafeArray&);    // 建構函式     SafeArray(int);     // 解構函式     ~SafeArray();     int get(int);     void set(int, int);     // 重載=運算子     SafeArray& operator=(const SafeArray&);private:    int *_array;     bool isSafe(int i); };``

• SafeArray.cpp
``#include "SafeArray.h"// 複製建構函式 SafeArray::SafeArray(const SafeArray &safeArray)                          : length(safeArray.length) {    _array = new int[safeArray.length];     for(int i = 0; i < safeArray.length; i++) {        _array[i] = safeArray._array[i];    }}// 重載=指定運算子 SafeArray& SafeArray::operator=(const SafeArray &safeArray) {    if(this != &safeArray) {        length = safeArray.length;        // 先清除原有的資源         delete [] _array;         _array = new int[safeArray.length];        for(int i = 0; i < safeArray.length; i++) {            _array[i] = safeArray._array[i];        }     }     return *this;}// 動態配置陣列SafeArray::SafeArray(int len) {    length = len;    _array = new int[length];}// 測試是否超出陣列長度bool SafeArray::isSafe(int i) {    if(i > length || i < 0) {        return false;    }     else {        return true;    }}// 取得陣列元素值int SafeArray::get(int i) {    if(isSafe(i)) {        return _array[i];    }     return 0;}// 設定陣列元素值void SafeArray::set(int i, int value) {    if(isSafe(i)) {        _array[i] = value;    }}// 刪除動態配置的資源SafeArray::~SafeArray() {    delete [] _array;}``