未调用c ++副本分配和移动分配

时间:2017-11-18 07:33:24

标签: c++ copy-assignment move-assignment-operator

我正在尝试实现复制和移动分配,但我不明白我应该如何使用它们。我已阅读以下主题
When did copy assignment operator called?
但它对我不起作用。

类别:

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;
};

我想知道何时调用复制赋值和移动赋值。提前谢谢。

2 个答案:

答案 0 :(得分: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

从这里你可以看到你的副本分配不正确,因为它仍然打印“alex”,但我想你只对所调用的函数感兴趣,而不是他们所做的。

答案 1 :(得分:1)

我的第一条评论,我建议删除所有*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");中出现了问题(使用已删除的复制构造函数)。

复制构造函数用于使用dir创建的临时实例初始化Directory("alex")

这很容易改变:

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

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

...编译

错误Directory dir2;中出现问题。

是的。您定义了构造函数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;
}

现在,它编译并运作。

输出:

cp assigment
done
Deleting was called
Deleting was called

您可以在ideone上看到它。