3 Replies - 480 Views - Last Post: 24 July 2011 - 11:18 AM Rate Topic: -----

#1 mahor1989  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 13-July 09

Please explain the following function

Posted 24 July 2011 - 09:37 AM

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 this function means?
Is This A Good Question/Topic? 0
  • +

Replies To: Please explain the following function

#2 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Please explain the following function

Posted 24 July 2011 - 09:45 AM

Google: operator overloading.
Was This Post Helpful? 0
  • +
  • -

#3 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 1645
  • View blog
  • Posts: 3,121
  • Joined: 30-May 10

Re: Please explain the following function

Posted 24 July 2011 - 10:22 AM

Or while you're at it, read up on forum posting etiquette.

Read this
http://www.catb.org/...ions.html#forum

So you don't do this
http://cboard.cprogr...g-function.html
Was This Post Helpful? 0
  • +
  • -

#4 Hezekiah  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 207
  • View blog
  • Posts: 550
  • Joined: 12-July 09

Re: Please explain the following function

Posted 24 July 2011 - 11:18 AM

This is overloading the * (multiplication) operator for the vector type. You probably saw it in a situation similar to this:
struct vector {
    int x, y;
    vector(int newX, int newY) : x(newX), y(newY) {}
    int operator*(vector &v) {
        return x * v.x + y * v.y;
    }
};

Now you can write something like:
vector v1(1, 2), v2(3, 4);
int result = v1 * v2;

This gives result the value 11, which is:
v1.x * v2.x + v1.y * v2.y

(this is called the dot product, but that isn't important now)
The first part of this page explains operator overloading.

This post has been edited by Hezekiah: 24 July 2011 - 11:19 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1