未调用 c++ 复制赋值和移动赋值

c++ copy assignment and move assignment are not being called

我正在尝试实现复制和移动分配,但我不明白应该如何使用它们。我已阅读以下主题

但它对我不起作用。

Class:

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

我想知道什么时候调用复制赋值和移动赋值。提前致谢。

也许这样试试:

#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" 但我猜您只对被调用的函数感兴趣,而不是它们的作用。

我是第一条评论,我建议删除所有 *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;.

有问题

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

上看到直播