一元'++'运算符的运算符重载 [英] Operator overload for unary '++' operator

查看:112
本文介绍了一元'++'运算符的运算符重载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我发现重载++运算符很困难。我使用Dev C ++编译器,我正在尝试使用Matrix用户定义的类型。所有其他重载都是有效的,除了A ++之外。其中A是Matrix类型。甚至++ A也是如此。运作正常。按照我的说法,相同的描述应该适用于A ++。也。任何建议??


/ *这是op ++的重载声明* /

Hi,
I am finding difficulty in overloading ''++'' operator. I use Dev C++ compiler and I am trying it with Matrix user defined type. All other overloads are functional except "A++" where A is type Matrix. Even "++A" is functioning fine. As per me the same description should work for "A++" also. Any suggestions??


/*This is the overload Declaration for op ++*/

展开 | 选择 | Wrap | 行号

推荐答案

这可能是您的编译器。使用Turbo C ++和Borland编译器,单个运算符重载适用于A ++和++ A.对于其他人,它可能没有。在由Stroustrup设计的orignal C ++中,你需要这样做:

operator ++(int);

这里int参数自动被0替换。参数帮助编译器区分表单。
This may be your compiler. Single operator overloading works for both A++ and ++A with Turbo C++ and Borland compiler. For others it may not. In orignal C++ designed by Stroustrup for post fix you need to do like:
operator ++(int);
Here int argument is automatically replaced by a 0. This extra argument helps compiler to distinguish between to forms.


首先,根据该代码,你在[i] ++ mat的末尾缺少一个'']''字符 [j; 声明。但我希望你已经抓住了它。


我看到的另一个重要的事情是,默认情况下,++运算符返回对其对象的引用(在这种情况下) ,矩阵&),无效。这样做的原因是,当在其他语句的中间使用时,您需要提供其他功能以及可以使用的东西。例如,
First, according to that code, you''re missing a '']'' character at the end of your [i]++mat[j; statement. But I expect you''ve already caught that.

The other, big thing I see is that, by default, the ++ operators return a reference to their object (in this case, a Matrix&), not void. The reason for this is that, when used in the middle of other statements, you need to give the other functions and whatnot something to work with. For instance,
展开 | 选择 | Wrap | 行号


亲爱的Ganon11,

正如你的建议,我试过,

Dear Ganon11,
As you suggested, I tried,

展开 | 选择 | Wrap | 行号


这篇关于一元'++'运算符的运算符重载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆