I have come acros开发者_开发问答s a function definition starting as:
int operator*(vector &y)
{
// body
}
After putting *
just after operator and before opening brace of argument, what does this function mean?
This is an operator *
overload. The syntax you should use is *(y)
while y
is of type vector
.
It allows you a reference like implementation, something similar to pointer reference in C. Of course the actual meaning depends on the body. e.g. you can return a reference to an internal element in the vector.
This is a function overload for the *
operator.
Its function overloading which overload the de-reference operator *
.
It is either a dereferencing operator or a multiplication operator override. It is dereferencing if it is in a namespace and multiplication if it is inside a class. Since it has a body and no class scope I will also assume that it is a dereferencing.
Actually its not a deferencing operator as in *ptr! Its actually an operator such as a multiplication operator. Here is a simple example
#include <iostream>
using namespace std;
struct Int{
int val;
Int(const int val = 0) : val(val){}
int operator*(const Int& number)const{
return val * number.val;
}
};
int main(){
Int n(4), m(5);
cout << n * m << endl; //use the operator*() implicitly
cout << (n.operator*(m)) << endl; //use the operator* explicitly
}
To define a de-ferenceing operator, its prototype would be operator*(). Look here for more information. Here is a live code to test.
精彩评论