English 中文(简体)
c++ 经营者超负荷多变
原标题:c++ polymorphism of operator overloading

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.

这里是法典

#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;
    }
问题回答

你对法典的模糊提及基本上是不可能的。 回答你的问题“我能够把纯粹的虚拟功能变成一个操作员+(功能)”,根本不秘密,例如考虑以下三边方案:

#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;
}

该编码将按预期汇编和操作简单细微和模版<>23。 不管怎样,你再做错事,这显然必须不同于这种情况(而且可能与操作者超负荷这一具体问题没有联系,只是虚拟的)。

<>strong>Edit: (根据评论,在你想要打上W/acode>const base&的情况下,在方法上添加const;——注意到其他答复也遗漏了const;以及,也根据评论:

如果你也想做<条码>15 + b,那么仅为此目的增加一项独立职能,即在<条码>> > > 封顶/条码>之前:

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

如果你正在寻找标准操作员+()执行,那么不幸的是,不可能:

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

该法典无法编纂,因为汇编者不能按价值退还十类摘要。

注:问题已经更新,使得这一答案无效。

如果你重新描述是正确的,你就重新开始使用<代码>virtual。 B. 确定经营者为虚拟......的关键词

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

我看到你应该处理的两个问题,或者至少能更好地了解两个问题。 这两条oil倒了这样一个事实,即你没有在你的基类中解决纯虚拟声明问题,ana:

1> 操作员+() 页: 1 具体来说,<代码>ana:operator+()是一个非固定的+操作者(只接收一个歌剧),而baba:operator+(baba& ali)baba上的双向操作者(接受两个歌剧)。 你们需要决定如何使用和使用。 如果你想要使用双程+(在<条码>baba<>>>>上给出定义,则你在<条码>中宣布:

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

baba:

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

由于<条码>x的定义在<条码>ana<>/条码>上,因此为什么需要成为<条码>的纯粹虚拟方法。 如果你有其他不同种类的衍生产品,则会引人注意(可能会导致通勤)。 但是,我期待你有自己的理由,因此我再次赢得问题。

2) operator=(ana&): You also have a problem with the assignment operator declarations. Again, you re not resolving the pure virtual. In ana you have: virtual void operator=(ana&)=0; while in baba you 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 ana to:

virtual void operator=(const ana&)=0

baba:

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

我对你为什么要宣布这纯粹是虚拟表示同样关切,因为我再次认为,不同的衍生类别将不同地加以实施,从而产生有意义的行为。 我再次肯定你有其原因。

我确实希望这一帮助。

纯虚拟功能的辛迪加就是:

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

但请注意,你很少想这样做——你通常希望超载<条码>操作器+<>条码>,作为自由功能加以执行,以便允许左轮操作(如有必要)转换。

 #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?




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签