2017-11-18 101 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