2011-10-04 62 views
4

請在下面找到我的代碼,我曾經調用移動構造函數(代碼從其他網站啓發),並讓我知道它有什麼問題,我正在使用GCC 4.5.3移動構造函數沒有在C++中調用0x

#include <iostream> 
#include <vector> 

class Int_Smart_Pointer { 

    int *m_p; 

public: 
    Int_Smart_Pointer() { 
    std::cout<<"Derfault Constructor"<< std::endl; 
    m_p = NULL; 
    } 

    explicit Int_Smart_Pointer(int n) { 
    std::cout<<"Explicit Constructor: " << n <<std::endl; 
    m_p = new int(n); 
    } 

    Int_Smart_Pointer(const Int_Smart_Pointer& other) { 
    std::cout<<"Copy Constructor: "<<std::endl; 
    if(other.m_p) 
     m_p = new int(*other.m_p); 
    else 
     m_p = NULL; 
    } 

    Int_Smart_Pointer(Int_Smart_Pointer&& other) { 
    std::cout<<"Move Constructor: "<<std::endl; 
    m_p = other.m_p; 
    other.m_p = NULL; 
    } 

    Int_Smart_Pointer& operator=(const Int_Smart_Pointer& other) { 
    std::cout<<"Copy Assignment"<< std::endl; 
    if(this != &other) { 
     delete m_p; 
     if(other.m_p) 
      m_p = new int(*other.m_p); 
     else 
      m_p = NULL; 
    } 

     return *this; 
    } 

    Int_Smart_Pointer& operator=(Int_Smart_Pointer&& other) { 
    std::cout<<"Move Assignment"<< std::endl; 
    if(this != &other) { 
     delete m_p; 
     m_p = other.m_p; 
     other.m_p = NULL; 
    } 

     return *this; 
    } 

    ~Int_Smart_Pointer() { 
    std::cout<<"Default Destructor"<< std::endl; 
    delete m_p; 
    } 

    int get() const{ 
    if(m_p) 
     return *m_p; 
    else 
     return 0; 
    } 

}; 

Int_Smart_Pointer square(const Int_Smart_Pointer& r) { 
    const int i = r.get(); 
    return Int_Smart_Pointer(i * i); 
} 

Int_Smart_Pointer getMove_Constructor() { 
    return Int_Smart_Pointer(100); 
} 


int main() 
{ 
    Int_Smart_Pointer a(10); 
    Int_Smart_Pointer b(a); 
    b = square(a); 

    std::cout<< std::endl; 

    Int_Smart_Pointer c(30); 
    Int_Smart_Pointer d(square(c)); //Move constructor Should be called (rvalue) 

    std::cout<< std::endl; 

    Int_Smart_Pointer e(getMove_Constructor()); //Move constructor Should be called (rvalue) 


    std::cout<< std::endl; 

    std::vector<Int_Smart_Pointer> ptr_vec; 
    ptr_vec.push_back(getMove_Constructor()); 
    ptr_vec.push_back(getMove_Constructor()); 

    std::cout<< std::endl; 

    return 0; 
} 

,輸出是

Explicit Constructor: 10 
Copy Constructor: 
Explicit Constructor: 100 
Move Assignment 
Default Destructor 

Explicit Constructor: 30 
Explicit Constructor: 900 

Explicit Constructor: 100 

Explicit Constructor: 100 
Move Constructor: 
Default Destructor 
Explicit Constructor: 100 
Move Constructor: 
Move Constructor: 
Default Destructor 
Default Destructor 

Default Destructor 
Default Destructor 
Default Destructor 
Default Destructor 
Default Destructor 
Default Destructor 
Default Destructor 

當我們使用std ::移動,同時施工,它是調用移動構造函數。

Int_Smart_Pointer d(std::move(square(c))); //used std::move and Move constructor called 
Int_Smart_Pointer e(std::move(getMove_Constructor())); //used std::move works as above 

但是,即使我們不使用,gerMove_Constructor和方形的返回值變爲右值而構建的對象,因爲我們找不到地址空間或引用他們,

請讓我知道什麼是錯的我的理解,如果不是那麼爲什麼移動構造函數不被調用。

在此先感謝。 Satya

回答

0

顯然,必須在squaregetMove_Constructor之內調用採用int的構造函數。 顯然,在Int_Smart_Pointer d(square(c))Int_Smart_Pointer e(getMove_Constructor())中,沒有其他構造函數被調用,因爲它們可以被編譯器優化掉。

+0

謝謝你henrik, Int_Smart_Pointer(int && n){ std :: cout <<「移動構造函數:」<< std :: endl; m_p = new int(n); } 我得到一個錯誤和 顯然,需要將現有構造函數(int n)更改爲(int&n) 並解決問題。 –

7

當通過值返回一個局部變量時,編譯器仍然允許像C++ 03(返回值優化)那樣複製構造函數。