C++ 使用重载+;派生类中的运算符

C++ 使用重载+;派生类中的运算符,c++,inheritance,C++,Inheritance,我从派生类(在我的例子中是飞机)练习重载赋值运算符的使用,它工作得很好。 我遇到的问题是重载+运算符。它只需将两个飞机物体的发动机功率和座椅相加,然后再将飞行速度相加(粗体线)。当从派生类(Vehicle::operator+(rhs);)调用时,它确实会转到基类重载+运算符函数,但随后Vehicle变量temp会按它应该做的方式对所有内容进行求和,但由于某些原因,会立即调用析构函数,并且我的派生对象的基类属性保持为0。期待任何建议或建议 #include <iostream> us

我从派生类(在我的例子中是飞机)练习重载赋值运算符的使用,它工作得很好。 我遇到的问题是重载+运算符。它只需将两个飞机物体的发动机功率和座椅相加,然后再将飞行速度相加(粗体线)。当从派生类(Vehicle::operator+(rhs);)调用时,它确实会转到基类重载+运算符函数,但随后Vehicle变量temp会按它应该做的方式对所有内容进行求和,但由于某些原因,会立即调用析构函数,并且我的派生对象的基类属性保持为0。期待任何建议或建议

#include <iostream>
using namespace std;

class Vehicle {
protected:
    int engine_power;
    int seats;
public:
    Vehicle(int engine_power, int seats)
        : engine_power{engine_power},seats{seats}{
            cout << "Vehicle constructor" << endl;
        }

    Vehicle &operator=(const Vehicle &rhs){
            cout << "Vehicle assignment" << endl;
        if (this==&rhs) 
            return *this;
        else{
            engine_power=rhs.engine_power;
            seats=rhs.seats;
            return *this;
            }
        }
    Vehicle operator+(const Vehicle &rhs){
        Vehicle temp{0,0};
        temp.engine_power=engine_power+rhs.engine_power;
        temp.seats=seats+rhs.seats;
        return temp;
    }
    ~Vehicle() {
        cout << "Vehicle destructor" << endl;
    }
};

class Airplane : public Vehicle {
private:
    int flying_speed;
public:
    Airplane(int a, int b, int flying_speed)
        : Vehicle{a,b}, flying_speed{flying_speed}{
            cout << "Airplane constructor" << endl;
        }

    Airplane &operator= (const Airplane &rhs){
        cout << "Airplane assignment" << endl;
        if (this!=&rhs){
            Vehicle::operator =(rhs);
            flying_speed=rhs.flying_speed;
            return *this;
        } else
            return *this;
    }
    Airplane operator+ (const Airplane &rhs) {
        Airplane temp{0,0,0};
        **Vehicle::operator +(rhs);**
        **temp.flying_speed=flying_speed+rhs.flying_speed;**
        return temp;
    }
    ~Airplane() {
        cout << "Airplane destructor" << endl;
    }
};

int main(){
Airplane a1 {10,20,30};
Airplane a2 {10,22,33};
Airplane a3 = a1+a2;
return 0;
}
#包括
使用名称空间std;
等级车辆{
受保护的:
发动机功率;
国际席位;
公众:
车辆(内部发动机动力,内部座椅)
:发动机动力{发动机动力},座椅{座椅}{

cout正如注释中指出的,当在派生类中调用时,您不会存储基类的结果
Vehicle::operator+()
。下面是固定代码:

#include <iostream>
using namespace std;

class Vehicle {
protected:
    int engine_power;
    int seats;
public:
    Vehicle(int engine_power, int seats)
        : engine_power{engine_power},seats{seats}{
            //cout << "Vehicle constructor" << endl;
        }

    Vehicle operator+(const Vehicle &rhs){
        Vehicle temp{0,0};
        temp.engine_power=engine_power+rhs.engine_power;
        temp.seats=seats+rhs.seats;
        return temp;
    }
};

class Airplane : public Vehicle {
private:
    int flying_speed;
public:
    Airplane(int a, int b, int flying_speed)
        : Vehicle{a,b}, flying_speed{flying_speed}{
            //cout << "Airplane constructor" << endl;
        }
    Airplane operator+ (const Airplane &rhs) {
        Airplane temp{0,0,0};
        static_cast<Vehicle&>(temp) = Vehicle::operator +(rhs);
        temp.flying_speed=flying_speed+rhs.flying_speed;
        return temp;
    }
    void print() const {
        cout << " power " << engine_power 
             << " seats " << seats  
             << " speed " << flying_speed 
            << std::endl;
    }
};

int main(){
    Airplane a1 {10,20,30};
    Airplane a2 {10,22,33};
    Airplane a3 = a1+a2;
    a3.print();
    return 0;
}
#包括
使用名称空间std;
等级车辆{
受保护的:
发动机功率;
国际席位;
公众:
车辆(内部发动机动力,内部座椅)
:发动机动力{发动机动力},座椅{座椅}{

//无法确保在
main
s作用域的末尾调用析构函数?您真正关心的是什么?如果代码中有需要指出的特殊行,请不要使用标记格式。如果您阅读,请在这些行上添加注释(如
+
)它说应该使用相应的复合赋值操作符来实现它们。因此,
操作符+
函数应该使用
操作符+=
来实现。我认为这会使您的案例更简单。调用
车辆::操作符+()
不存储在任何地方。在
飞机::操作员+()
的定义中,您可能需要
飞机温度=车辆::操作员+(rhs);温度飞行速度=飞行速度+rhs飞行速度;返回温度;
更一般地说,您可能需要
车辆::操作员+()
使用一些涉及虚拟函数的逻辑来实际执行它们的工作。@AleksandarRadosavljevic——完全不必要的用户定义赋值运算符和析构函数的原因是什么?毫无理由地实现这些函数只会让你犯一些愚蠢的错误,可能会减慢代码的速度。针对你的类非常好,可以有效地复制,并且不会出错。另外,不要使用
*
突出显示代码——它们可能会被指针取消引用或指针声明所混淆。