-1

我正在尝试实现复制和移动分配,但我不明白应该如何使用它们。我已阅读以下主题
何时调用了复制赋值运算符?
但这对我不起作用。

班级:

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

我想知道何时调用复制分配和移动分配。提前致谢。

4

2 回答 2

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”,但我猜你只对被调用的函数感兴趣,而不是他们做什么。

于 2017-11-18T08:02:34.170 回答
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");:(使用已删除的复制构造函数)出了点问题。

复制构造函数用于初始化dirDirectory("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上看到它。

于 2017-11-18T08:25:00.147 回答