c++ - 子类调用父复制分配而不是移动分配?

你好我写了下面的代码,我有一个父类(Vehicule)和一个子类(Car),我正在尝试研究具有继承的移动/复制分配的行为。 但是有些事情我不太了解: 子类的移动赋值调用父类的移动赋值,照顾对象的父类部分(切片)

Car &Car::operator=(Car &&rhs)
{
    if (this != &rhs)
    {
        Vehicule::operator=(rhs);
        this->production = rhs.production;
        std::cout << "Car Move Assignment for: " << this->name << std::endl;
    }
    return *this;
}

但是由于我的对象是一个 R 值,我不明白为什么我的子类在创建对象时调用父 Copy 赋值而不是 Move 赋值;

Car audi;
audi= Car{"Audi_A3", 2023};

我使用了调试器和这行代码:

Vehicule::operator=(rhs);

带我去父类抄作业

Vehicule &Vehicule::operator=(const Vehicule &rhs)

代替:

Vehicule &Vehicule::operator=(Vehicule &&rhs)
#include <iostream>
#include <string>
#include <ostream>
#include <vector>

class Vehicule
{
protected:
    std::string name;

public:
    Vehicule();                               // No args constructor
    Vehicule(std::string name);               // overloaded constructor
    Vehicule(const Vehicule &source);         // copy constructor
    Vehicule(Vehicule &&source);              // move constructor
    Vehicule &operator=(const Vehicule &rhs); // copy assignment
    Vehicule &operator=(Vehicule &&rhs);      // move assignment
    ~Vehicule();
};

Vehicule::Vehicule() : name{"Vehicule"} {}
Vehicule::Vehicule(std::string name) : name{name} {}
Vehicule::~Vehicule() {}

// Copy constructor
Vehicule::Vehicule(const Vehicule &source) : name{source.name}
{
    std::cout << "Vehicule Copy constructor for: " << name << std::endl;
}

// Move constructor
Vehicule::Vehicule(Vehicule &&source) : name{source.name}
{
    std::cout << "Vehicule Move constructor for: " << source.name << std::endl;
}
// copy assignment
Vehicule &Vehicule::operator=(const Vehicule &rhs)
{
    if (this != &rhs)
    {
        this->name = rhs.name;
        std::cout << "Vehicule Copy assignment for: " << this->name << std::endl;
    }
    return *this;
}

// Move assignment
Vehicule &Vehicule::operator=(Vehicule &&rhs)
{
    if (this != &rhs)
    {
        this->name = rhs.name;
        std::cout << "Vehicule Move assignment for: " << this->name << std::endl;
    }
    return *this;
}

class Car : public Vehicule
{
    friend std::ostream &operator<<(std::ostream &os, const Car &car);
    friend std::istream &operator>>(std::istream &is, Car &car);

private:
    int production;

public:
    Car();                                 // No args constructor
    Car(std::string name, int production); // overloaded constructor
    ~Car();
    Car(const Car &source);         // Copy constructor
    Car(Car &&source);              // Move constructor
    Car &operator=(const Car &rhs); // Copy assignment
    Car &operator=(Car &&rhs);      // Move assignment
    std::string get_name() { std::cout << name << " ." << production << std::endl; };
};

Car::Car() : production{2000} {}

Car::Car(std::string name, int production) : Vehicule{name}, production{production} {}

Car::~Car() {}

// copy constructor
Car::Car(const Car &source) : Vehicule{source.name}, production{source.production}
{
    std::cout << "Car Copy Constructor for: " << name << std::endl;
}

// Move constructor
Car::Car(Car &&source) : Vehicule(source.name), production{source.production}
{
    std::cout << "Car Move Constructor for: " << name << std::endl;
}

Car &Car::operator=(const Car &rhs)
{
    if (this != &rhs)
    {
        Vehicule::operator=(rhs);
        this->production = rhs.production;
        std::cout << "Car Copy Assignment for: " << this->name << std::endl;
    }
    return *this;
}

Car &Car::operator=(Car &&rhs)
{
    if (this != &rhs)
    {
        Vehicule::operator=(rhs);
        this->production = rhs.production;
        std::cout << "Car Move Assignment for: " << this->name << std::endl;
    }
    return *this;
}
std::ostream
    &
    operator<<(std::ostream &os, const Car &car)
{
    os << car.name << " | " << car.production;
    return os;
}
std::istream &operator>>(std::istream &is, Car &car)
{
    std::cout << "Enter car and production: ";
    std::cin >> car.name >> car.production;
    return is;
}

int main(int argc, char const *argv[])
{
    // Car Move assignment & Vehicule move assignment.
    Car audi;
    audi= Car{"Audi_A3", 2023};

    return 0;
}

// OUTPUT 
// Vehicule Copy assignment for: Audi_A3
// Car Move Assignment for: Audi_A3

最佳答案

Vehicule::operator=(rhs);

这里,rhs 是一个左值,这导致调用复制赋值。为了调用移动赋值运算符,需要将其设为右值:

Vehicule::operator=(std::move(rhs));

https://stackoverflow.com/questions/73313869/

相关文章:

reactjs - 我可以给 react 查询一个超时(秒或毫秒......)所以在这段时间之后它会

google-cloud-platform - 我无法让 google cloud function

javascript - ECMA脚本 : What does `status` mean in S

snowflake-cloud-data-platform - 从 Snowflake 查询 del

scala - 计算字符串列表中的元音

r - 使用 dplyr R 中的 mutate with across 将不存在的列添加到数据框

python - 根据另一个的范围和类别填充 Pandas 列

amazon-web-services - 无法在 yum 上下载 caddy 包

javascript - 在映射函数中的 .then 内部分配变量

reactjs - 使用 React Router 将可选查询参数添加到动态路径