简体   繁体   English

从算术运算符返回时,为什么不能将此类作为引用传递?

[英]Why can't I pass this class as a reference when returned from arithmetic operators?

If I have a simple class like this: 如果我有一个像这样的简单班级:

template<typename T>
class coord
{
public:

    coord() : x(0), y(0)
    {
    }

    coord(T X, T Y) : x(X), y(Y)
    {
    }

    T x;
    T y;

    coord& operator-=(const coord& rhs)
    {
        (*this).x -= rhs.x;
        (*this).y -= rhs.y;
        return *this;
    }

    coord& operator+=(const coord& rhs)
    {
        (*this).x += rhs.x;
        (*this).y += rhs.y;
        return *this;
    }
};

Along with the following operators (they're not friend s because there's no private members to access). 与以下运算符一起(由于没有私有成员可访问,所以它们不是friend )。

template<typename T = int>
inline coord<T> operator-(coord<T> lhs, const coord<T>& rhs)
{
    lhs -= rhs;
    return lhs;
}

template<typename T = int>
inline coord<T> operator+(coord<T> lhs, const coord<T>& rhs)
{
    lhs += rhs;
    return lhs;
}

Elsewhere in my code I have another class A with a method that looks like this: 在代码的其他地方,我还有另一个类A ,其方法如下所示:

void A::SetVarC(coord<int>& c)
{
    m_c = c;
}

(assume there's a getter for m_c as well) (假设还有一个用于m_c的吸气剂)

When I try to invoke this method using the addition and subtraction operators I overloaded: 当我尝试使用加减运算符调用此方法时,我重载了:

int x = 1;
int y = 1;

A* a = new A();

coord c1(1,2);

a->SetVarC(c1 - a->GetVarC() + coord<int>(x,y));

I get an error that there's no known conversion from coord<int> to coord<int>& . 我收到一个错误,没有从coord<int>coord<int>&已知转换。 I can see that my subtraction and addition operators aren't returning references, but I thought that wouldn't matter. 我可以看到我的减法和加法运算符没有返回引用,但是我认为那没关系。 I am using C++11... are move semantics coming into play here? 我正在使用C ++ 11 ...移动语义在这里起作用吗?

Temporary cannot be bind to non const reference, change SetVarC to 临时不能绑定到非const引用,将SetVarC更改为

void A::SetVarC(const coord<int>& c)
{
    m_c = c;
}

or 要么

void A::SetVarC(coord<int> c)
{
    m_c = std::move(c);
}

You are passing a temporary coord<int> object to A::SetVarC() which requires a non-const reference, which is not possible. 您正在将临时coord<int>对象传递给A::SetVarC() ,该对象需要非常量引用,这是不可能的。

You should fix your code by changing A::SetVarC() to accept a const coord<int>& . 您应该通过更改A::SetVarC()来接受const coord<int>&来修复代码。

You're creating arithmetic operators with a side affect... These operators shouldn't change the value of the arguments used. 您正在创建具有副作用的算术运算符...这些运算符不应更改所用参数的值。

And, to the answer your question, these methods return a temporary object, that can't be passed as reference to SetVarC . 并且,为回答您的问题,这些方法返回一个临时对象,该对象不能作为对SetVarC引用SetVarC

template<typename T = int>
inline coord<T> operator-(const coord<T>& lhs, const coord<T>& rhs)
{
    coord<T> res(lhs)
    res -= rhs;
    return res;
}

template<typename T = int>
inline coord<T> operator+(const coord<T>& lhs, const coord<T>& rhs)
{
    coord<T> res(lhs)
    res += rhs;
    return res;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM