C ++中运算符重载中的参数数量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15441633/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Number of arguments in operator overload in C++
提问by Lemurr
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.
我正在学习 C++,并创建了两个简单的 hello-world 应用程序。在他们两个中,我都使用运算符重载,但这是问题所在。在第一个上,我可以为重载运算符提供两个参数,这很好。
Header:
标题:
enum Element {a,b,c,d,e};
Element operator + (Element x, Element y);
//more overloads for -, *, / here
Source:
来源:
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:
但是在我的第二个应用程序(简单的复数计算器)中 - 这种方法不起作用。在谷歌搜索并弄清楚原因之后,我最终得到了以下代码:
Header:
标题:
struct Complex {
double Re;
double Im;
Complex (double R, double I) : Re(R), Im(I) { }
Complex operator + (Complex &Number);
//more overloads
};
Source:
来源:
Complex Complex::operator + (Complex &Number)
{
Complex tmp = Complex(0, 0);
tmp.Re = Re + Number.Re;
tmp.Im = Im + Number.Im;
return tmp;
}
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?
它现在正在工作,但我想知道,为什么在第一段代码中我被允许在operator
重载中放置两个参数,但是在第二个参数中我得到了以下错误?
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?
无论我是否使用类,都是一样的。我一直在寻找许多文档,第二种方法似乎更正确。也许是因为不同的参数类型?
Both sources compiled with -Wall -pedantic
parameters using g++
, both are using the same libraries.
使用-Wall -pedantic
参数编译的两个源代码g++
都使用相同的库。
回答by Jon Purdy
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 +
.
有四种方法可以定义二元运算符,例如+
.
As a free function with access to only the
public
members of the class:// 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)
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)
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)
作为只能访问
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)
作为成员函数,可以访问类的所有成员:
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
函数,可以访问类的所有成员: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 相同: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)
回答by taocp
Since + is a binary operator, if you overload it inside a struct/class, you can only provide one more operand, the reason is that the first operand is implicitly the calling object. That is why in the first case, you have two parameters since it is outside scope of your class/struct, while in the second case, it was overloaded as member function.
由于 + 是二元运算符,如果在结构体/类中重载它,则只能再提供一个操作数,原因是第一个操作数隐式是调用对象。这就是为什么在第一种情况下,您有两个参数,因为它超出了类/结构的范围,而在第二种情况下,它作为成员函数被重载。
回答by NPE
If you prefer that operator+
takes both operands as explicit arguments, it must be defined as a free (i.e. non-member) function:
如果您更喜欢operator+
将两个操作数都作为显式参数,则必须将其定义为自由(即非成员)函数:
class Complex {
friend Complex operator+(const Complex& lhs, const Complex& rhs);
}
Complex operator+(const Complex& lhs, const Complex& rhs) {
...
}
You haveto use this form if the left operand is of a primitive type, or of a class that you don't control (and thus can't add a member function to).
您必须使用这种形式如果左操作数是基本类型,或一类,你不控制(因此不能添加一个成员函数)的。
回答by CODError
If overloaded function is a member function to the class, the we pass only one argument, and there is one hidden parameter (thispointer) that points to other object required to perform binary operation like '+'. thispointer points to one of the operands and calls the overloaded function; while other operand is passed as an argument. Example:
如果重载函数是类的成员函数,则我们只传递一个参数,并且有一个隐藏参数(this指针)指向执行二进制操作(如“+”)所需的其他对象。this指针指向操作数之一并调用重载函数;而其他操作数作为参数传递。例子:
class ExampleClass
{
public:
int x;
//a this pointer will be passed to this function
ExampleClass& operator+(const ExampleClass& o){ return x+o.x; }
};
ExampleClass obj1, obj2, obj;
obj = obj1 + obj2; //the overloaded function is called as obj1.operator+(obj2) internally
//this pointer is passed to the function
When the overloaded function is not a member function (either a free function or a friend function), then we don't have the thispointer provided to the overloaded function. In this case, the compiler expects two arguments to the function which are used as operands.
当重载函数不是成员函数(自由函数或友元函数)时,我们没有提供给重载函数的this指针。在这种情况下,编译器需要函数的两个参数作为操作数。
class ExampleClass
{
public:
int x;
//this pointer will not be passed to this function
friend ExampleClass& operator+(const ExampleClass& o1, const ExampleClass& o2){ return o1.x+o2.x; }
};
obj = obj1 + obj2; //the overloaded function is called as operator+(obj1, obj2) internally
回答by vivek
e1 + e2 == e1.operator+(e2) this means e1 is a object and operator+ is a member and e2 is as a variable .basicaly oops permits us to do just write e1 + e2 compiler automatically understand as a e1.operator+(e1)
e1 + e2 == e1.operator+(e2) 这意味着 e1 是一个对象,operator+ 是一个成员,e2 是一个变量。 )