为什么+运算符重载返回类型是类类型不是整数? [英] Why does + operator overloading return type is class type not integer?

查看:166
本文介绍了为什么+运算符重载返回类型是类类型不是整数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在本文中,作者选择返回类型为类类型 http://www.learncpp.com/cpp-tutorial/92-overloading-the-arithmetic-operators/ 强调的文本,我们可以只更改返回类型return int ,因为我想做以下,我试过这个,它只是工作正常,为什么作者做了返回类类

In this article the author chose the return type to be class type http://www.learncpp.com/cpp-tutorial/92-overloading-the-arithmetic-operators/emphasized text ,Can we just change the return type to return int, because i wanted to do the following , i tried this and it just worked fine , why did the author made the return type class ??

#include <cstdlib>
#include <iostream>
using namespace std;
class Cents // defining new class
{
    private:
    int m_Cents;
    int m_Cents2;
    public:
    Cents(int Cents=0, int Cents2=0) // default constructor
    { 
        m_Cents=Cents;
        m_Cents2=Cents2;
}
Cents(const Cents &c1) {m_Cents = c1.m_Cents;}
friend ostream& operator<<(ostream &out, Cents &c1); //Overloading << operator
friend int operator+(const Cents &c1, const Cents &c2); //Overloading + operator
};
ostream& operator<<(ostream &out, Cents &c1)
{
out << "(" << c1.m_Cents << " , " << c1.m_Cents2 << ")" << endl;
return out; 
}
int operator+(const Cents &c1, const Cents &c2)
{
return ((c1.m_Cents + c2.m_Cents) + (c1.m_Cents2 + c2.m_Cents2 ));
}
int main(int argc, char *argv[])
{
Cents cCents(5, 6);
Cents bCents;
bCents = cCents;
cout << bCents << endl;
Cents gCents(cCents + bCents, 3);
cout << gCents << endl;
system ("PAUSE");
return 0;
}


推荐答案

一件事要记住的是,在相同类型的两个对象之间发生的添加的结果总是非常特定类型的。所以它可能为你工作,但在逻辑上是不正确的。
其次,你不能执行嵌套的 + 语句,而不返回类类型。
例如,如果你想这样做。

Apart from many other things, one thing to remember is that the result of the addition taking place between two objects of the same type is always of that very specific type. So it might work for you but logically it is incorrect. Secondly, you are unable to perform nested + statements with not returning the class type. For example if you want to do this.

Obj1 + Obj2 + Obj3 ;

你会得到一个编译时错误。
原因是 + 运算符的重载函数应该返回相同类类型的值的结果。下面写的操作符也可以写为函数调用如下。

You would get a compile time error. The reason is that the overloaded function for + operator should return the result by value of the same class type. The operators written below could also be written for the function call as follows.

Obj1 + Obj2 ;

等效于...

Obj1.operator+(Obj2) ;

对于嵌套添加操作,可以这样做。

For the nested addition operation, you do this way.

Obj1 + Obj2 + Obj3 ;

相当于....

(Obj1.operator+(Obj2)).operator+(Obj3) ;
|---------------------|                       

这里,这部分...

(Obj1.operator+(Obj2))

类对象,使用 Obj3 作为参数调用下一个方法。所以如果你不从 + 函数返回类对象,这个部分将是一个 int 而不是一个对象。 + 函数不会调用该int或任何其他非类数据类型。因此,它会给你一个错误。

becomes another temporary class object on which the next method gets called with Obj3 as a parameter. So if you do not return class object from + function, this part would be an int instead of an object. The + function would not get called on that int or any other non-class data type. So it would give you an error.

简而言之,建议始终通过Value返回一个对象,从重载的 + 函数。

In a nutshell, it is advisable to always return an object by Value, from overloaded + function.

这篇关于为什么+运算符重载返回类型是类类型不是整数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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