首页 > 解决方案 > 移动构造函数和移动重载赋值运算符的问题?

问题描述

fredoverflow(user 237K Rep.) 在他的两个答案中解释了大多数事情

但是在实现移动构造函数和重载的移动赋值运算符(OMAO)时(我在整个问题中都使用这些简短的形式)我遇到了一些问题,我将在这里提出。

用户 Greg Hewgill(拥有 826K 代表的用户)还有另一个答案 https://stackoverflow.com/a/3106136/11862989his
我在引用他的话,

假设你有一个返回实体对象的函数,那么普通的 C++ 编译器将为 multiply() 的结果创建一个临时对象,调用复制构造函数来初始化 r,然后销毁临时返回值。C++0x 中的移动语义允许调用“移动构造函数”通过复制其内容来初始化 r,然后丢弃临时值而不必破坏它。

我也会提到这个问题。

好的,现在我开始

代码

.cpp

#include"34_3.h"
#include<iostream>
#include<conio.h>
#include<cstring>

A::A()                                       // O arg ctor
{
    std::cout<<"0 arg constructor\n";
    p=0;
    s=nullptr;
        
}

A::A(int k1,const char *str)                 // 2 arg ctor
{
    std::cout<<"2 arg constructor\n";
    
    p=k1;
    s=new char[strlen(str)+1];
    strcpy(s,str);
    
}

A::A(const A &a)                             // copy ctor
{
    std::cout<<"copy constructor\n";
    
    p=a.p;
    s=new char[strlen(a.s)+1];
    strcpy(s,a.s);
    
}

A::A(A &&a)                                   // Move ctor
{
    std::cout<<"Move constructor\n";
    
    p=a.p;
    s=new char[strlen(a.s)+1];
    strcpy(s,a.s);
    a.s=nullptr;
    
}


A& A::operator=(const A &a)                 // Overloaded assignement opeator `OAO`
{
    std::cout<<"overloade= operator\n";
    
    p=a.p;
    s=new char[strlen(a.s)+1];
    strcpy(s,a.s);
    return *this;
    
}


A& A::operator=(A &&a)                        // `OMAO`
{
    std::cout<<"Move overloade = operator\n";
    
    p=a.p;
    s=new char[strlen(a.s)+1];
    strcpy(s,a.s);
    a.s=nullptr;
    return *this;
    
}

A::~A()                                       // Dctor
{
    delete []s;
    std::cout<<"Destructor\n";
    
}

void A::display()
{
    std::cout<<p<<" "<<s<<"\n";
    
}

。H

#ifndef header
#define header

struct A
{
    private:
        int p;
        char *s;
    public:
        A();                            //  0 arg ctor
        A(int,const char*);             //  2 arg ctor
        A(const A&);                    //  copy ctor
        A(A&&);                         //  Move ctor
        
        A& operator=(const A&);         // `OAO`
        A& operator=(A&&);              // `OMAO`
        
        ~A();                           // dctor
        
        void display(void);
        
};
#endif

我在这里放了几个主要功能及其输出,以便我可以轻松地讨论这个问题。

1_main

A make_A();
int main()
{
    A a1=make_A();
    
    a1.display();
    
}
A make_A()
{
    A a(2,"bonapart");
    return a;
    
}

输出

2 arg constructor
2 bonapart
Destructor
  1. 为什么它不执行 Move 构造函数,但是如果我在 .cpp 文件中注释了 Move 构造函数定义并在 .h 文件中声明,那么它会给出错误[Error] no matching function for call to 'A::A(A)',如果我使用它A a1=std::move(make_A());,那么 Move 构造函数会调用,那么为什么会发生这种情况?
  2. 为什么amake_A() 函数中对象的析构函数没有运行?

2_main()

A make_A();
int main()
{
    A a1;
    a1=make_A();
    
    a1.display();
    
}
A make_A()
{
    A a(2,"bonapart");
    return a;
    
}

输出

0 arg ctor
2 arg ctor
Move overloade = operator
copy ctor
Dctor
Dctor
2 bonapart
Dctor
  1. 现在,复制构造函数和析构函数针对由于从 Move 重载 = 运算符函数返回 *this 而创建的临时对象运行。根据 Greg Hewgill 声明C++ 0x,允许调用 Move 构造函数以通过复制其内容进行初始化,然后丢弃临时值而不必破坏它。我正在使用C++11但仍在初始化是通过创建临时对象、复制构造函数来完成的。
  2. 我没有得到第二个析构函数正在运行的对象?

3_main

fredoverflow(用户 237K Rep.)保留了 Move 重载运算符的返回类型,A&但我认为这是错误的。

A make_A();
int main()
{
    A a1,a2;
    a2=a1=make_A();
    
    a1.display();
    a2.display();
    
}
A make_A()
{
    A a(2,"bonapart");
    return a;
    
}

输出

[Error] prototype for 'A& A::operator=(A&&)' does not match any in class 'A'

所以我觉得返回类型应该是A&&或者AA&&也给出错误[ERROR] can't bind a lvalue to a&&

所以返回类型必须是A,对吗?

4

在 Move 构造函数和 Move 重载 = 运算符中,我使用a.s=nullptr;了此语句始终在 Move 语义中使用 fredoverflow(user) 解释了类似“现在源不再拥有它的对象”但我没有得到它。因为如果我不写这个声明仍然没有问题,一切正常。请解释这一点

标签: c++movedestructormove-semanticsrvalue-reference

解决方案


您的班级A有几个问题:

  • 您的赋值运算符不处理自赋值和泄漏:

    A& A::operator=(const A& a)
    {
        std::cout<<"overload operator=\n";
        if (this != &a)
        {
            p = a.p;
            delete[] s;
            s = new char[strlen(a.s) + 1];
            strcpy(s, a.s);
        }
        return *this;
    }
    
  • 您的移动不会移动,而是复制:

A::A(A&& a) : p(a.p), s(a.s)
{
    a.s = nullptr;
    std::cout << "Move constructor\n";
}

A& A::operator=(A&& a)
{
    std::cout << "Move overload operator=\n";

    if (this != &a) {
        p = a.p;
        delete [] s;
        s = a.s;
        a.s = nullptr;
    }
    return *this;
}

现在,关于

A make_A()
{
    A a(2,"bonapart"); // Constructor
    return a;
}

由于潜在的复制省略(NRVO),有几种情况(gcc有-fno-elide-constructors控制它的标志)

如果 NRVO 适用,则a“就地”构造,因此不会发生额外的破坏/移动;

否则有一个移动构造函数和a.

A make_A()
{
    A a(2,"bonapart"); // #2 ctor(int const char*)
    return a; // #3 move (elided with NRVO)
} // #4 destruction of a, (elided with NRVO)

int main()
{
    A a1; // #1: default ctor
    a1 = // #5: Move assignment (done after make_A)
      make_A(); // #6: destructor of temporary create by make_A

    
    a1.display();
} // #8: destructor of a1

使用 NRVO

default ctor
ctor(int const char*)
move assignment
destructor
display
destructor

没有 NRVO ( -fno-elide-constructors)

default ctor
ctor(int const char*)
move ctor
destructor
move assignment
destructor
display
destructor

演示

为了

A a1,a2;
a2 = a1 = make_A();

a1 = make_A();使用移动分配。 a2 = (a1 = make_A())使用复制分配作为移动分配返回(正确)A&

4 在 Move 构造函数和 Move 重载 = 运算符中,我使用a.s=nullptr;了此语句始终在 Move 语义中使用 fredoverflow(user) 解释了类似“现在源不再拥有它的对象”但我没有得到它。因为如果我不写这个声明仍然没有问题,一切正常。请解释这一点

您的问题是您进行复制而不是移动。

如果你这样做s = a.s;而不是副本

s = new char[strlen(a.s) + 1];
strcpy(s, a.s);

然后两者都this->sa.s指向相同的数据,并且两者都thisa在其析构函数中释放(相同的)内存->双重释放错误。

a.s = nullptr;会解决这个问题。


推荐阅读