为什么我的操作符重载处理左对象和右对象? [英] Why does my operator overloading handle left versus right objects?

查看:114
本文介绍了为什么我的操作符重载处理左对象和右对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于操作符重载的问题。下面是我的示例代码。

I had a question on operator overloading. Below is my the example code. If you can read through it and my question is below.

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

// main

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



我理解,当您尝试使用操作符重载将对象添加到对象时,是相同的。

I understand that when you attempt to add to objects together using operator overloading they should be the same.

这是问题:
我最近看到当对象在编译的运算符的一侧,但是在另一方面它没有。如:

Here is the question: I recently saw when the object was on one side of the operator it compiled but when it was on the other it didn't. Such as:

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

它不工作,但只是更容易构造问题与代码。)

(Also I know in my example it doesn't work either way but was just easier to frame question with code.)

如果操作符重载编译时,对象是在操作符的一侧,但不是

How does operator overloading compile when the object is on one side of the operator but not compile when it is on the other.

感谢

推荐答案

code> t = r + 1; 表示 t = r.operator +(1); / code>定义匹配的运算符+()方法,否则表示 t = operator +(r,1); 。它不会编译,因为您没有定义任何 + 运算符,它在左侧有一个示例 $ c> int 在右边,例如:

t = r + 1; means t = r.operator+(1); if r defines a matching operator+() method, otherwise it means t = operator+(r, 1); instead. It does not compile because you did not define any + operator that takes an example on the left and an int on the right, eg:

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

或:

// 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&)方法,例如:

If you had defined a contructor that takes an int as input, the compiler could have created a temp example when you pass an int value to the example::operator+(const example&) method, eg:

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; code>表示 t = operator +(1,r); (因为1不是类类型)。它不编译,因为你没有定义一个全局 + 运算符,它在左侧有一个 int c $ c>例如在右侧:

Likewise, t = 1 + r; means t = operator+(1, r); (since 1 is not a class type). It does not compile because you did not define a global + operator that takes an int on the left and an example on the right:

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

这篇关于为什么我的操作符重载处理左对象和右对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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