C++ 使派生类使用重写运算符

C++ 使派生类使用重写运算符,c++,C++,我有以下代码: #include <iostream> using namespace std; class Base { public: Base operator/(const Base& other){ Base res; cout << "Base /" << endl; return res; } Base& operator/=(const Base&am

我有以下代码:

#include <iostream>

using namespace std;


class Base {
public:
    Base operator/(const Base& other){
        Base res;
        cout << "Base /" << endl;
        return res;
    }
    Base& operator/=(const Base& other){
        cout << "Base /=" << endl;
        return *this;
    }
};

class Derived : public Base {
public:
    Derived operator/(const Derived& other){
        Derived res;
        cout << "Derived /" << endl;
        return res;
    }
};

int main() {
    Derived d1, d2;
    Base b1, b2;
    b1 = d1 / d2;
    b2 = d1 /= d2;
}
class Base {
public:
    virtual Base operator/(const Base& other) const {
        Base res;
        cout << "Base /" << endl;
        return res;
    }
    Base& operator/=(const Base& other){
        cout << "Base /=" << endl;
        *this = *this / other;
        return *this;
    }
};

class Derived : public Base {
public:
    virtual Base operator/(const Base& other) const override {
        cout << "Derived /(Base&)" << endl;
        if(dynamic_cast<const Derived*>(&other)==0) {
            // do something specific here, or call base operator:
            return Base::operator/(other);
        }
        else {
            return operator/(dynamic_cast<const Derived&>(other));
        }
    }
    Derived operator/(const Derived& other) const {  // cannot override 
        cout << "Derived /(Derived&)" << endl;
        return *this;
    }
};

int main() {
    Derived d1, d2;
    Base b1, b2;

    b1 = d1 / d2;
    // Output: Derived /(Derived&)

    b2 = d1 /= d2;
    // Output:
    // Base /=
    // Derived /(Base&)
    // Derived /(Derived&)

    return 0;
}
#包括
使用名称空间std;
阶级基础{
公众:
基本运算符/(常量基本和其他){
基本资源;

cout虽然显然不是首选的方式,可能有点“丑陋”,但可以使用
派生::运算符/
,而不提供
派生::运算符/=
。为此,需要执行以下步骤:

  • Base::operator/=
  • 将运算符
    /
    设置为虚拟,以便它不会静态绑定在
    /=
    -实现中
  • 在类
    派生的
    中提供一个
    运算符/
    -实现,该实现重写
    基中的运算符,即除了
    派生运算符/(常量派生的&)
    之外,还提供一个运算符
    虚拟基运算符/(常量基&)
    (注意,后者不能重写,因为它具有协变参数类型)
  • 在这个
    Derived::operator/(const Base&other)
    -实现中,检查
    other
    的动态类型,并显式调用相应的实现
  • 请参阅以下代码:

    #include <iostream>
    
    using namespace std;
    
    
    class Base {
    public:
        Base operator/(const Base& other){
            Base res;
            cout << "Base /" << endl;
            return res;
        }
        Base& operator/=(const Base& other){
            cout << "Base /=" << endl;
            return *this;
        }
    };
    
    class Derived : public Base {
    public:
        Derived operator/(const Derived& other){
            Derived res;
            cout << "Derived /" << endl;
            return res;
        }
    };
    
    int main() {
        Derived d1, d2;
        Base b1, b2;
        b1 = d1 / d2;
        b2 = d1 /= d2;
    }
    
    class Base {
    public:
        virtual Base operator/(const Base& other) const {
            Base res;
            cout << "Base /" << endl;
            return res;
        }
        Base& operator/=(const Base& other){
            cout << "Base /=" << endl;
            *this = *this / other;
            return *this;
        }
    };
    
    class Derived : public Base {
    public:
        virtual Base operator/(const Base& other) const override {
            cout << "Derived /(Base&)" << endl;
            if(dynamic_cast<const Derived*>(&other)==0) {
                // do something specific here, or call base operator:
                return Base::operator/(other);
            }
            else {
                return operator/(dynamic_cast<const Derived&>(other));
            }
        }
        Derived operator/(const Derived& other) const {  // cannot override 
            cout << "Derived /(Derived&)" << endl;
            return *this;
        }
    };
    
    int main() {
        Derived d1, d2;
        Base b1, b2;
    
        b1 = d1 / d2;
        // Output: Derived /(Derived&)
    
        b2 = d1 /= d2;
        // Output:
        // Base /=
        // Derived /(Base&)
        // Derived /(Derived&)
    
        return 0;
    }
    
    类基{
    公众:
    虚拟基运算符/(常数基和其他)常数{
    基本资源;
    
    cout
    operator/
    operator/=
    是不同的。不,您不能。您必须为派生类实现/=。代码中没有重写的函数(只有重载的函数)您可以将
    operator/=
    设置为虚拟函数,并将
    operator/
    设置为调用它的非成员。不过,我认为这不是常见的做法(通常不会对派生层次结构使用太多运算符重载)@ForceBru,也许我遗漏了一些东西,但我说过它们是一样的吗?实际上我在
    Base
    中实现了这两个。