c ++运算符重载的多态性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2969966/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 11:37:53  来源:igfitidea点击:

c++ polymorphism of operator overloading

c++operator-overloadingpolymorphismabstract-class

提问by hasan

How can I make pure virtual function a operator+(); function. wheh ? do like this in base class int operator+()=0; compiler gives error . in derive class operator+() function compiler say that derive class cannot make . because following class is abstract I know that I cannot create object of abstract classes but now I try to make derive class object.

如何使纯虚函数成为 operator+(); 功能。什么时候?在基类中这样做 int operator+()=0; 编译器给出错误。在派生类 operator+() 函数编译器中说派生类不能 make 。因为下面的类是抽象的,我知道我不能创建抽象类的对象,但现在我尝试创建派生类对象。

Here is the code

这是代码

#include <iostream>
using namespace std;
class ana {
    protected :
    int x;

    public :
    virtual int operator+()=0;
    virtual void operator=(ana&)=0;
    };

class baba : public ana{
    public:
    baba(int k){x=k;}
    int   operator+(baba &ali){
        int k;
        k=x+ali.x;
        return k;
    }
   void operator=(baba &ali){
       x=ali.x;
       }


    };

int main(){
    baba k(4);

    return 0;
    }

回答by Alex Martelli

Your vague mentions of code are essentially impossible to follow. Answering your question "How can I make pure virtual function a operator+(); function", there's absolutely no secret to it, e.g. consider the following trivial program:

您对代码的含糊提及基本上无法遵循。回答您的问题“如何使纯虚函数成为运算符+(); 函数”,这绝对没有秘密,例如考虑以下简单程序:

#include <iostream>

class base {
public:
  virtual int operator+(int) const = 0;
};

class deriv: public base {
public:
  int operator+(int) const {return 23;}
};

int main() {
  deriv d;
  base& b = d;
  std::cout << b + 15 << std::endl;
  return 0;
}

This compiles and runs just fine and emits 23as expected. Whatever it is that you're doing wrong, obviously it must therefore be different from this (and probably not connected to the specific issue of having an operator overload be pure virtual).

这编译并运行得很好,23并按预期发出。无论您做错了什么,显然它必须与此不同(并且可能与运算符重载纯虚拟的特定问题无关)。

Edit: (as per comments, added constto the method just in case you want to call it w/a const base&-- note that other answers have also omitted this const; and, also per comments):

编辑:(根据评论,添加const到方法中,以防万一您想用 w/a 调用它const base&——请注意,其他答案也省略了这一点const;并且,也根据评论):

If you want to be able to also do 15 + b, just add a free-standing function for that very purpose, say just before main:

如果您也想这样做15 + b,只需为此目的添加一个独立的功能,就在之前说main

inline int operator+(int i, const base&b) {
    return b + i;
}

回答by Kornel Kisielewicz

If you are searching for a standard operator+() implementation, then unfortunately that is impossible:

如果您正在寻找标准的 operator+() 实现,那么不幸的是,这是不可能的:

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

This code cannot compile, because the compiler cannot return an abstract X class by value.

此代码无法编译,因为编译器无法按值返回抽象 X 类。

回答by Klaim

Note : Question have been updated, making this answer less valid.

注意:问题已更新,使此答案不太有效。

If you're description is correct, you're just forgetting to use the virtualkeyword to specify the operator as virtual...

如果您的描述是正确的,那么您只是忘记使用virtual关键字将运算符指定为虚拟...

class Addable{
public:
    virtual int operator+ const ( const Addable& other ) = 0; // pure virtual
    virtual int operator+ const ( int number ) = 0; // pure virtual
};

回答by andand

I see two issues that you should address, or at the very least better understand. They both boil down to the fact that you have not resolved the pure virtual declaration in your base class, ana:

我认为您应该解决或至少更好地理解两个问题。它们都归结为您尚未解析基类中的纯虚拟声明的事实ana

1) operator+():Your class babadefines a different + operator than the base class. In particular, ana::operator+()is a unary + operator (accepts only one operand), while baba::operator+(baba& ali)is a binary operator (accepts two operands) on baba. You need to decide which to use and use it. If you want to use the binary + (which given your definition in babais what I think you want) then you declare in ana:

1) operator+():您的类baba定义了与基类不同的 + 运算符。特别是,ana::operator+()是一元 + 运算符(仅接受一个操作数),而baba::operator+(baba& ali)是二元运算符(接受两个操作数) on baba。您需要决定使用哪个并使用它。如果你想使用二进制 + (给出你的定义baba是我认为你想要的),那么你声明ana

virtual int operator+(const ana&) const =0;

and in baba:

并在baba

virtual int operator+(const ana& ali) const {return x+ali.x; }  

Why this needs to be a pure virtual method in ana, since xis defined in anais curious. It will do interesting things if you have other derived classes that do things differently (losing commutivity is likely to result). But I expect you have your reasons, so I won't question further.

为什么这需要是 中的纯虚方法ana,因为x在中定义ana是奇怪的。如果您有其他以不同方式做事的派生类,它将做有趣的事情(可能会导致失去可交换性)。但我希望你有你的理由,所以我不会进一步质疑。

2) operator=(ana&):You also have a problem with the assignment operator declarations. Again, you're not resolving the pure virtual. In anayou have: virtual void operator=(ana&)=0;while in babayou have: void operator=(baba &ali). The arguments are different since baba&is not the same as ana&; so the pure virtual declaration is again not resolved. In order to fix this, you probably want to change the declaration in anato:

2) operator=(ana&):赋值运算符声明也有问题。同样,您不是在解析纯虚拟。在ana你有: virtual void operator=(ana&)=0;而在baba你有:void operator=(baba &ali)。参数不同,因为baba&ana&;不同。所以纯虚拟声明再次没有解决。为了解决这个问题,您可能希望将声明更改ana为:

virtual void operator=(const ana&)=0

and in baba:

并在baba

virtual void operator=(const ana& ali) { x=ali.x; }

I have similar concerns as to why you want to declare this as a pure virtual since, again, it assumes that different derived classes will implement it differently leading to interesting behavior. Again, I'm sure you have your reasons.

对于为什么要将其声明为纯虚拟,我也有类似的担忧,因为它再次假设不同的派生类会以不同的方式实现它,从而导致有趣的行为。再说一次,我相信你有你的理由。

I do hope this helps.

我希望这会有所帮助。

回答by hasan

 #include 
using namespace std;
class ana {
    protected :
    int x;
    public :

virtual int operator+()=0; virtual void operator=(ana&)=0; };

class baba:public ana{ public: baba(int k){x=k;} int operator+(baba &ali){ int k; k=x+ali.x; return k; } void operator=(baba &ali){ x=ali.x; }

};

int main(){ baba k(4);

return 0; }<code>what is wrong here?

回答by Jerry Coffin

The syntax for a pure virtual function would be something like:

纯虚函数的语法类似于:

class X { 
public:
     virtual int operator+(X const &rhs) = 0;
};

Note, however, that you only rarely want to do this -- you typically want an overload of operator+to be implemented as a free function, to allow conversion of the left operand (if necessary).

但是请注意,您很少想这样做——您通常希望将 的重载operator+实现为一个自由函数,以允许左操作数的转换(如有必要)。