2017-11-18 100 views
-1

我想實現複製和移動任務,但我不明白我應該如何使用它們。我已閱讀以下主題
When did copy assignment operator called?
但它沒有爲我工作。C++複製分配和移動分配不被稱爲

類:

class Directory{ 

    string name; 
public: 
     Directory(string name):name(name) { 

     } 
     ~Directory() { 
      cout << "Deleting was called" <<endl; 

      } 

     Directory& operator=(Directory& other){ 
      cout << "cp assigment" <<endl; 
      return *this; 
     } 
     Directory& operator=(Directory&& other){ 
      cout << "move assigment" <<endl; 
      return *this; 
     } 
}; 

主要

int main() 
{ 

    Directory* dir = new Directory("alex"); 
    Directory* dir2; 
    dir = dir2; 

    cout<<"done"<<endl; 
}; 

我想知道拷貝賦值和分配移動時調用。提前致謝。

+0

賦值是針對您的案例中的指針('Directory *')完成的。刪除'*'和'new',它會起作用。 – Scheff

+1

你正在使用指針,你會如何期望副本被稱爲? – user0042

+1

'dir = dir2;'調用內置的指針複製賦值運算符,而不是您的類複製賦值運算符。 – VTT

回答

1

我的第一個評論,我建議刪除所有* s和new

因此,主要功能變成:

int main() 
{ 
    Directory dir = Directory("alex"); 
    Directory dir2; 
    dir2 = dir; // <-- fixed, original was: dir = dir2; 

    cout<<"done"<<endl; 
    return 0; // <-- fixed, return is strictly recommended for every non-void function 
} 

編譯...

錯誤:東西是錯誤的Directory dir = Directory("alex");(刪除拷貝構造函數的使用)。使用由Directory("alex")創建的臨時實例初始化dir

這是很容易改變:

int main() 
{ 
    Directory dir("alex"); // <-- fixed: direct construction 
    Directory dir2; 
    dir2 = dir; 

    cout<<"done"<<endl; 
    return 0; 
} 

編譯...

錯誤:東西是錯誤的Directory dir2;

A yepp。您定義了構造函數Directory(string name);。這將禁止在此處需要的默認構造函數的自動創建。

我們要麼可以在默認的構造函數添加到class Directory

Directory() = default; 

,或者我們可以改善現有的非默認的構造函數,以便它可以被使用的默認構造函數:

Directory(string name = string()): name(name) { } 

整個來源:

#include <iostream> 
#include <string> 
using namespace std; 

class Directory{ 

    string name; 
public: 
     Directory(string name = string()):name(name) { 

     } 
     ~Directory() { 
      cout << "Deleting was called" <<endl; 

      } 

     Directory& operator=(Directory& other){ 
      cout << "cp assigment" <<endl; 
      return *this; 
     } 
     Directory& operator=(Directory&& other){ 
      cout << "move assigment" <<endl; 
      return *this; 
     } 
}; 

int main() { 
    //Directory dir = Directory("alex"); 
    Directory dir("alex"); 
    Directory dir2; 
    dir2 = dir; 

    cout<<"done"<<endl; 
    // your code goes here 
    return 0; 
} 

現在,它編譯es和作品。

輸出:

cp assigment 
done 
Deleting was called 
Deleting was called 

你可以看到它住在ideone

1

也許嘗試這樣的:

#include <iostream> 
#include <string> 

using namespace std; 

class Directory{ 
public: 
     string name; 

     Directory() { 
      cout << "Constructor 1 was called" <<endl;    
     } 

     Directory(string name):name(name) { 
      cout << "Constructor 2 was called" <<endl;    
     } 

     ~Directory() { 
      cout << "Deleting was called" <<endl; 
     } 

     Directory(const Directory& other){ 
      cout << "cp cons" <<endl; 
     } 

     Directory& operator=(const Directory& other){ 
      cout << "cp assigment" <<endl; 
      return *this; 
     } 

     Directory& operator=(Directory&& other){ 
      cout << "move assigment" <<endl; 
      return *this; 
     } 
}; 


int main() 
{ 

    Directory dir = Directory("alex"); 
    Directory dir2; 
    dir2 = dir; 

    cout << "done " << dir.name << dir2.name << endl; 
}; 

我改變了代碼,以便它不使用指針,增加額外的構造函數(注意拷貝構造函數),並增加了一些額外的印刷。

我得到這樣的輸出:

Constructor 2 was called 
Constructor 1 was called 
cp assigment 
done alex 
Deleting was called 
Deleting was called 

從這裏就可以看到你的拷貝賦值是不正確的,它仍然打印「亞歷克斯」,但我猜你是隻在功能感興趣被調用,而不是他做。

+0

謝謝你的努力。真的行。我試圖從這個例子中學習。我不明白爲什麼你的代碼有效,而且我的工作不正常。編輯後的主要部分是相同的。 –

+0

@AlexLavriv - 你是否像我一樣添加了一個拷貝構造函數? – 4386427

+0

@AlexLavriv - 此鏈接可能會對您有所幫助:https://stackoverflow.com/a/9945598/4386427 – 4386427