首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >为什么我的操作符重载处理左对右对象?

为什么我的操作符重载处理左对右对象?
EN

Stack Overflow用户
提问于 2014-03-05 21:14:37
回答 3查看 88关注 0票数 0

我有个关于操作员超载的问题。下面是我的示例代码。如果你能读懂,我的问题在下面。

代码语言:javascript
运行
复制
//class definition
class example
{
private:
    int a ; // Defined in FeetInches.cpp
public:
    void seta(int f)
    {
        a = f;
    }
    example operator + (const example &); // Overloaded +

    int geta()
    {
        return a;
    }
};

example example::operator + (const example &right)
{
    example temp;

    temp.a = a + right.a;
    return temp;
}

//主要

代码语言:javascript
运行
复制
#include "header" //this is the class definition above
#include <iostream>
using namespace std;

int main()
{
    example r;
    r.seta(1);

    example s;
    s.seta(1);

    example t;
    t = r + s;
    t = r + 1;  //if included it won't compile
    t = 1 + r; //if included it won't compile

    int x = t.geta();
    cout << x;

    cin.get();
    return 0;
}

我理解,当您试图使用操作符重载将对象添加到一起时,它们应该是相同的。

下面是一个问题:我最近看到了,当对象在它编译的操作符的一边时,而在另一边时,它没有编译。

它编译的t = r + 1;t = 1 + r;,它没有。

(我也知道,在我的示例中,这两种方法都不起作用,只是更容易用代码来构建问题。)

当对象位于操作符的一边时,操作符重载是如何编译的,而当对象位于另一边时,操作符重载是如何编译的。

谢谢

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2014-03-05 21:33:07

如果t = r + 1;定义了匹配的operator+()方法,则意味着t = r.operator+(1);,否则就意味着t = operator+(r, 1);。它没有编译,因为您没有定义任何+操作符,这些操作符的左边是example,右边是int,例如:

代码语言:javascript
运行
复制
// as a class method:

class example
{
    ...
public:
    ...
    example operator + (int right) const;
};

example example::operator + (int right) const
{
    example temp;
    temp.seta(a + right);
    return temp;
}

或者:

代码语言:javascript
运行
复制
// as a global operator:

class example
{
    ...
public:
    ...
    friend example operator + (const example& left, int right);
};

example operator + (const example& left, int right)
{
    example temp;
    temp.seta(left.a + right);
    return temp;
}

如果您已经定义了一个以int作为输入的构造器,那么当您将int值传递给example::operator+(const example&)方法时,编译器可能已经创建了一个临时int,例如:

代码语言:javascript
运行
复制
class example
{
    ...
public:
    example (int f);
    ...
    example operator + (const example& right);
};

example::example::(int f)
    : a(f)
{
}

example example::operator + (const example& right)
{
    example temp;
    temp.a = a + right.a;
    return temp;
}

同样,t = 1 + r;的意思是t = operator+(1, r); (因为1不是类类型)。它没有编译,因为您没有定义一个全局+操作符,该操作符在左边接受一个int,在右边定义一个example

代码语言:javascript
运行
复制
class example
{
    ...
public:
    ...
    friend example operator + (int left, const example& right);
};

example operator + (int left, const example& right)
{
    example temp;
    temp.a = left + right.a;
    return temp;
}
票数 -1
EN

Stack Overflow用户

发布于 2014-03-05 21:27:51

如果您详细地重写了违规语句,则如下所示:

代码语言:javascript
运行
复制
t.operator=(1.operator+(r));

这没有多大意义,也让编译器感到困惑。

由于混淆,它可以将数字1转换为example的实例,也可以将变量r转换为整数。不幸的是,您在课堂上也没有提供足够的信息。

如果您为类提供了一个接受整数的构造函数,那么情况可能不会那么令人困惑:

代码语言:javascript
运行
复制
class example
{
  public:
    example(int new_value) : a(new_value)
      { ; }
};

现在,您已经为编译器提供了一个将整数转换为example的方法。

另一种选择是提供一个浇铸或转换操作符:

代码语言:javascript
运行
复制
class example
{
  public:
    int operator int (const example& e)
    {
      return e.a;
    }
};

还有其他选择,例如创建一个接受整数的加法。

编辑1:

如果您正在设计一个单元类,请注意常量。你不知道常数在什么单位,如英尺或英寸。编译器将无法提供帮助,因为数值常量没有与它们关联的单元。

票数 0
EN

Stack Overflow用户

发布于 2014-03-05 21:29:52

这很简单,当您重载操作符时,您可以通过两种方式来完成:

首先就像你做的那样。您为类插入了重载运算符。这样,左参数必须是该类的对象。为什么?因为当您调用这个运算符时,您可以将其作为类中每个函数的调用来执行。您不能以其他方式进行此操作,因为您不能通过其他类型调用此函数。

第二,您让重载的操作符作为类的朋友。在你的课堂上,你写了这句话:

代码语言:javascript
运行
复制
 friend example& operator + (const example &left, const example &right);

在你的课程定义之后,你把这个:

代码语言:javascript
运行
复制
example& operator + (const example &left, const example &right){...}

因此,如果要添加整数或其他类型,则必须修改运算符以如下方式添加:

代码语言:javascript
运行
复制
example& operator + (const example &left, const int right){...}

或者这个:

代码语言:javascript
运行
复制
example& operator + (const int left, const example &right){...}

如果您想从操作器的左边或硬边添加int,请选择其中一个或两者。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22209434

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档