hi,
Can anyone teach me something about overloading operators ? and what is the purpose of it ? and can you give me very simple example so that I can easily understand it ?
Thanks
curious mind
hi,
Can anyone teach me something about overloading operators ? and what is the purpose of it ? and can you give me very simple example so that I can easily understand it ?
Thanks
curious mind
Its very useful for handling your own user defined objects. For example I have a class Vector3d which comprises of x, y, and z values. Now in order to add vectors you need to add the x's, add the y's and add the z's. This is the purpose of operator overloading. Here's the code for the + operator: (the x,y, and z values are in an array)
Hope that clears it up a bit...Code://declaration inside class friend Vector3d operator + ( const Vector3d& a, const Vector3d& b ); //definition Vector3d operator +( const Vector3d& a, const Vector3d& b ) { Vector3d temp; int i; for( i = 0; i < 3; i++ ) { temp.xyz[ i ] = b.xyz[ i ] + a.xyz[ i ]; } return temp; }
Couldn't think of anything interesting, cool or funny - sorry.