C ++中运算符重载中的参数数量 [英] Number of arguments in operator overload in C++
问题描述
我正在学习C ++,并创建了两个简单的hello-world应用程序.在这两种方法中,我都使用运算符重载,但这是问题所在.在第一个上,我可以为重载运算符提供两个参数,这很好.
I'm learning C++ and I created two simple hello-world applications. In both of them I use operator overload, but here is the problem. On the first one, I can provide two arguments to overload operator, and it's fine.
标题:
enum Element {a,b,c,d,e};
Element operator + (Element x, Element y);
//more overloads for -, *, / here
来源:
Element operator + (Element x, Element y) {
return ArrayOfElements[x][y];
}
但是在我的第二个应用程序(简单的复数计算器)中,此方法无效.谷歌搜索并弄清楚原因之后,我得到了以下代码:
But in my second app (simple complex number calculator) - this method didn't work. After googling and figuring out why, I end up with this code:
标题:
struct Complex {
double Re;
double Im;
Complex (double R, double I) : Re(R), Im(I) { }
Complex operator + (Complex &Number);
//more overloads
};
来源:
Complex Complex::operator + (Complex &Number)
{
Complex tmp = Complex(0, 0);
tmp.Re = Re + Number.Re;
tmp.Im = Im + Number.Im;
return tmp;
}
它现在正在工作,但是我想知道,为什么在第一段代码中允许我在operator
重载中放置两个参数,而在第二个代码中却出现以下错误? strong>
It's working now, but I want to know, why in the first piece of code I was allowed to put two arguments in operator
overloading, but with the second one I was given the following error?
complex.cpp:5:51: error: 'Complex Complex::operator+(Complex, Complex)' must take either zero or one argument
无论我是否使用类,它都是一样的.我一直在寻找许多文档,第二种方法似乎更正确.也许是因为参数类型不同?
It's the same whenever I use classes or not. I've been seeking through many docs and the second way seem to be more correct. Maybe it's because of different argument types?
两个使用g++
用-Wall -pedantic
参数编译的源都使用相同的库.
Both sources compiled with -Wall -pedantic
parameters using g++
, both are using the same libraries.
推荐答案
假设您有一个这样的类:
Suppose you have a class like this:
class Element {
public:
Element(int value) : value(value) {}
int getValue() const { return value; }
private:
int value;
};
有四种方法来定义二进制运算符,例如+
.
There are four ways to define a binary operator such as +
.
-
作为自由函数,只能访问该类的
public
成员:
// Left operand is 'a'; right is 'b'.
Element operator+(const Element& a, const Element& b) {
return Element(a.getValue() + b.getValue());
}
e1 + e2 == operator+(e1, e2)
作为成员函数,可以访问该类的所有成员:
As a member function, with access to all members of the class:
class Element {
public:
// Left operand is 'this'; right is 'other'.
Element operator+(const Element& other) const {
return Element(value + other.value);
}
// ...
};
e1 + e2 == e1.operator+(e2)
作为friend
函数,可以访问该类的所有成员:
As a friend
function, with access to all members of the class:
class Element {
public:
// Left operand is 'a'; right is 'b'.
friend Element operator+(const Element& a, const Element& b) {
return a.value + b.value;
}
// ...
};
e1 + e2 == operator+(e1, e2)
作为在类主体外部定义的friend
函数-行为与#3相同:
As a friend
function defined outside the class body—identical in behaviour to #3:
class Element {
public:
friend Element operator+(const Element&, const Element&);
// ...
};
Element operator+(const Element& a, const Element& b) {
return a.value + b.value;
}
e1 + e2 == operator+(e1, e2)
这篇关于C ++中运算符重载中的参数数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!