子类调用父级复制分配而不是移动分配?

ASM*_*der 2 c++ inheritance c++11

您好,我编写了下面的代码,我有一个父类(车辆)和一个子类(汽车),我正在尝试研究具有继承的移动/复制赋值的行为。然而,有一些我不太明白:子类的移动赋值调用父类的移动赋值,以处理对象的父类部分(切片)

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;
}
Run Code Online (Sandbox Code Playgroud)

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

Car audi;
audi= Car{"Audi_A3", 2023};
Run Code Online (Sandbox Code Playgroud)

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

Vehicule::operator=(rhs);
Run Code Online (Sandbox Code Playgroud)

带我去父班抄作业

Vehicule &Vehicule::operator=(const Vehicule &rhs)
Run Code Online (Sandbox Code Playgroud)

代替 :

Vehicule &Vehicule::operator=(Vehicule &&rhs)
Run Code Online (Sandbox Code Playgroud)
#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

Run Code Online (Sandbox Code Playgroud)

Sam*_*hik 5

Vehicule::operator=(rhs);
Run Code Online (Sandbox Code Playgroud)

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

Vehicule::operator=(std::move(rhs));
Run Code Online (Sandbox Code Playgroud)