简体   繁体   English

C ++中的运算符重载和多态

[英]Operators overloading and polymorphism in C++

I'm writing a simple wrapper to basic numeric types in C++. 我正在为C ++中的基本数字类型编写一个简单的包装。

I want to have an abstract base class Number and a few derived classes like Short, Integer, Long and Double. 我想要一个抽象的基类Number和一些派生类,例如Short,Integer,Long和Double。

For the Number class to be abstract I need to declare some pure virtual methods that are common for all numeric types, for example basic operators (+, -, *, /): 为了使Number类变得抽象,我需要声明一些对于所有数字类型都通用的纯虚方法,例如基本运算符(+,-,*,/):

class Number {
    public:
    [...]
        virtual Number operator+(Number) = 0;
        virtual Number operator-(Number) = 0;
        virtual Number operator*(Number) = 0;
        virtual Number operator/(Number) = 0;
    [...]
}

but I'm afraid this will not work when I will try to implement that operators in derived classes but with the concret types like: 但是,当我尝试在派生类中使用具体类型的操作符来实现该操作符时,恐怕这将不起作用:

class Integer : public Number {
    public:
    [...]
        Integer operator+(Integer);
        Integer operator-(Integer);
        Integer operator*(Integer);
        Integer operator/(Integer);
    [...]
}

when I try to compile this code I get this kind of messages: 当我尝试编译此代码时,会收到以下消息:

Number.hpp error: cannot declare parameter 'anonymous' to be of abstract type 'Number' note: because the following virtual functions are pure within 'Number': note: virtual Number Number::operator+(Number) Number.hpp错误:无法将参数“ anonymous”声明为抽象类型“ Number”注意:因为以下虚拟函数在“ Number”中是纯净的:注意:虚拟Number Number :: operator +(Number)

I'm sure I can do this, but so far I have no idea how. 我敢肯定我可以做到,但是到目前为止我还不知道怎么做。 Could you please help me a little? 你能帮我一下吗? ;) ;)

1) You must return a reference (or pointer), since Number is abstract 1)您必须返回一个引用(或指针),因为Number是抽象的
2) The parameter must also be a reference (same reason) 2)参数也必须是引用(相同原因)
3) the parameter to the derived class must be the same 3)派生类的参数必须相同
4) you are missing 'virtual' on the overrides 4)您在替代项上缺少“虚拟”

Here's a hack that kinda works: 这是一种可行的技巧:

class Number {
    public:
        virtual Number& operator+(const Number&) = 0;
};

class Integer : public Number {
    public:
        virtual Integer& operator+(const Number&) override;
    private:
        int i;
};

Integer& Integer::operator+(const Number& n)
{
    static Integer i;
    const Integer* param = dynamic_cast<const Integer*>(&n);
    assert(param);
    i.i = this->i + param->i;
    return i;
}

Note, returning a reference to a static can surprise you in some cases, see comments. 注意,在某些情况下,返回静态引用可能会让您感到惊讶,请参阅注释。
Also, polymorphism might not be the best fit for what you are trying to do, for example, 同样,多态可能不是最适合您尝试做的事情,例如,
do you really need to keep track of numbers, that you don't know what type they are? 您是否真的需要跟踪数字,不知道数字是什么类型?

If you really want polymorphism here, smartpointers should probably accompany the solution 如果您确实要在此处使用多态,则智能指针可能应该与解决方案一起提供

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM