JavaScript EditorFree JavaScript Editor     Ajax Editor 



Main Page
  Previous Section Next Section

Function and Operator Overloading

The last topic I want to talk about is overloading, which comes in two flavors: function overloading and operator overloading. I don't have time to explain operator overloading in detail, but I'll give you a general example. Imagine that you have your Vector3D class and you want to add two vectors, v1 + v2, and store the sum in v3. You might do something like this:

Vector3D v1 = {1,3,5} ,
         v2 = {5,9,8} ,
         v3 = {0,0,0} ;

// define an addition function, this could have
// been a class function
Vector3D Vector3D_Add(Vector3D v1, Vector3D v2)
{
Vector3D sum; // temporary used to hold sum

sum.x = v1.x+v2.x;
sum.y = v1.y+v2.y;
sum.z = v1.z+v2.z;

return(sum);

} // end Vector3D_Add

Then, to add the vectors with the function, you would write the following code:

v3 = Vector3D_Add(v1, v2);

It's crude, but it works. With C++ and operator overloading, you can actually overload the + operator and make a new version of it to add the vectors! So you can write this:

v3 = v1+v2;

Cool, huh? The syntax of the overloaded operator function follows, but you'll have to read a C++ book for details:

class Vector3D
{
public:

int x,y,z; // anyone can mess with these

// this is a member function
int length(void)  {return(sqrt(x*x + y*y + z*z)); }

// overloaded the + operator
Vector3D operator+(Vector3D &v2)
{
Vector3D sum; // temporary used to hold sum
sum.x = x+v2.x;
sum.y = y+v2.y;
sum.z = z+v2.z;

return(sum);
}

private:
    int reference_count; // this is hidden

} ;

Notice that the first parameter is implicitly the object, so the parameter list has only v2. Anyway, operator overloading is very powerful. With it, you can really create new data types and operators so that you can perform all kinds of cool operations without making calls to functions.

You already saw function overloading when I was talking about constructors. Function overloading is nothing more than writing two or more functions that have the same name but different parameter lists. Let's say you want to write a function called Plot Pixel that has the following functionality: If you call it without parameters, it simply plots a pixel at the current cursor position, but if you call it with an x,y, it plots a pixel at the position x,y. Here's how you would code it:

int cursor_x, cursor_y; // global cursor position

// the first version of Plot_Pixel
void Plot_Pixel(void)
{
// plot a pixel at the cursor position
plot(cursor_x, cursor_y);
}

////////////////////////////////

// the second version of Plot_Pixel
void Plot_Pixel(int x, int y)
{
// plot a pixel at the sent position and update
// cursor
plot(cursor_x=x, cursor_y=y);
}

You can call the functions like this:

Plot_Pixel(10,10); // calls version 2

Plot_Pixel(); // calls version 1

TRICK

The compiler knows the difference because the real name of the function is created not only by the function name, but by a mangled version of the parameter list, creating a unique name in the compiler's namespace.


      Previous Section Next Section
    



    JavaScript EditorAjax Editor     JavaScript Editor