navanshu
navanshu

Reputation: 59

Please explain the following function

I have come across 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?

Upvotes: 2

Views: 229

Answers (5)

Cem Kalyoncu
Cem Kalyoncu

Reputation: 14603

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.

Upvotes: 0

dchhetri
dchhetri

Reputation: 7136

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.

Upvotes: -2

NirMH
NirMH

Reputation: 4949

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.

Upvotes: 7

Summer_More_More_Tea
Summer_More_More_Tea

Reputation: 13456

Its function overloading which overload the de-reference operator *.

Upvotes: 0

linuxuser27
linuxuser27

Reputation: 7353

This is a function overload for the * operator.

Upvotes: 1

Related Questions