2013-04-25 146 views
4

這是我對一些C++ 11研究示例的實現。我讓所有的構造函數和析構函數打印到控制檯。但令人驚訝的是,我得到了構造函數兩次,但是析構函數被調用了三次。爲什麼由析構函數構造的兩個對象被調用三次

似乎意想不到的是0x7fff5fbff6d0。當這個對象被創建時?但爲什麼沒有構造函數調用相關聯?

爲什麼會發生這種情況?

template<typename T> 
class ArrayWrapper{ 
public: 
    ArrayWrapper():data_(nullptr), size_(0){ 
     cout << "Default ctor called "<< this <<endl; 
    } 

    ArrayWrapper(size_t n, const T& val) : data_(new T[n]), size_(n){ 
     cout << "ctor_n_val called "<< this << endl; 
     for_each(data_, data_+size_, [&](T& elem){ elem=val; }); 
    } 

    ArrayWrapper(const ArrayWrapper& other): data_(new T[other.size_]), size_(other.size_) 
    { 
     cout << "copy ctor called "<< this <<endl; 
     copy(other.data_, other.data_+other.size_, data_); 
    } 

    ArrayWrapper(ArrayWrapper&& other): data_(other.data_), size_(other.size_) 
    { 
     cout << "move ctor called"<<endl; 
     other.data_ = nullptr; 
     other.size_ = 0; 
    } 

    ArrayWrapper<T>& operator=(const ArrayWrapper& other){ 
     cout << "copy assignment called" <<endl; 
     if(this != &other){ 
      delete data_; 
      data_ = new T[other.size_]; 
      copy(other.begin(), other.end(), begin()); 
      size_ = other.size_; 
     } 
     return *this; 
    } 

    ArrayWrapper<T> operator=(ArrayWrapper&& other){ 
     cout << "move assignment called " <<this << " <- " <<&other <<endl; 
     swap(size_, other.size_); 
     swap(data_, other.data_); 
    } 

    ~ArrayWrapper(){ 
     cout <<"Destroying " << this << " Size " << size_ <<endl; 
    } 
    typedef T* iterator; 
    typedef const T* const_iterator; 

    T* begin() { 
     return data_; 
    } 

    T* end(){ 
     return data_ + size_; 
    } 

    const T* begin() const { 
     return data_; 
    } 

    const T* end() const { 
     return data_ + size_; 
    } 

    const T* cbegin() const { 
     return data_; 
    } 

    const T* cend() const { 
     return data_ + size_; 
    } 

    size_t size(){ 
     return size_; 
    } 
public: 
    T* data_; 
    size_t size_; 
}; 

template<typename T> 
ArrayWrapper<T> make_array(size_t n, const T& val){ 
    cout <<"Factory method called"<<endl; 
    return ArrayWrapper<T>(n, val); 
} 

template<typename T> 
std::ostream& operator<<(std::ostream& os, const ArrayWrapper<T>& arr){ 
    for(const T& elem: arr){ os << elem << ", ";} 
    return os; 
} 

int main(){ 
    size_t n = 10; 
    ArrayWrapper<int> a4(n, 1); 
    a4 = make_array(n, 4); // move assignment: 
    cout << "A4: " << a4 << endl; 
} 

輸出:

$ g++-mp-4.8 -std=c++11 move.cpp 

$ ./a.out 

ctor_n_val called 0x7fff5fbff6b0 

Factory method called 

ctor_n_val called 0x7fff5fbff6e0 

move assignment called 0x7fff5fbff6b0 <- 0x7fff5fbff6e0 

Destroying 0x7fff5fbff6d0 Size 0 

Destroying 0x7fff5fbff6e0 Size 10 

A4: 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 

Destroying 0x7fff5fbff6b0 Size 10 

回答

12

你我的行動賦值運算符應該返回一個參考:

ArrayWrapper<T>& operator=(ArrayWrapper&& other) 
//   ^

既然你有它的價值迴歸,但目前還沒有return說法,你調用未定義的行爲。你應該實現它像拷貝賦值運算符,除了當然移動的資源,而不是將它們複製:

ArrayWrapper<T>& operator=(ArrayWrapper&& other){ 
    if(this != &other){ 
     delete[] data_; 
     size_ = other.size_; 
     data_ = other.data_; 
     other.size_ = 0; 
     other.data_ = nullptr; 
    } 
    return *this; 
} 

另外,還要注意使用delete[]刪除動態分配數組。

+1

+1一旦返回類型被固定,並且'return * this;'被添加,則有2個構造函數調用和2個匹配的析構函數調用。 – Praetorian 2013-04-25 03:24:53

+0

我試過了。它現在產生預期的行爲 – xiaochuanQ 2013-04-25 03:26:18

+0

該移動賦值運算符也希望設置'other.size_ = 0;' – 2013-04-25 14:19:17

相關問題