Author Topic: How to use...  (Read 1657 times)

0 Members and 1 Guest are viewing this topic.

pkohut

  • Guest
How to use...
« on: May 30, 2010, 06:42:50 AM »
AcGePoint2d operator *(double val, const AcGePoint2d& p)

Can't find anything in my books or online about overloaded arithmetic operators with multiple parameters.  Hence, how would such a thing be used?

TIA.

pkohut

  • Guest
Re: How to use...
« Reply #1 on: May 30, 2010, 08:32:43 AM »
Ok, got it figured out. The original question should have been "how to use or implement in a new class" as I already know how to use it as defined in ARX.

It falls under the classification of "Binary operator overloading", per IBM's website http://publib.boulder.ibm.com/infocenter/lnxpcomp/v8v101/index.jsp?topic=/com.ibm.xlcpp8l.doc/language/ref/cplr323.htm
Quote
Overloading binary operators (C++ only)
You overload a binary unary operator with either a nonstatic member function that has one parameter, or a nonmember function that has two parameters. Suppose a binary operator @ is called with the statement t @ u, where t is an object of type T, and u is an object of type U. A nonstatic member function that overloads this operator would have the following form:
Code: [Select]
return_type operator@(T)A nonmember function that overloads the same operator would have the following form:
Code: [Select]
return_type operator@(T, U)An overloaded binary operator may return any type.

To make this work within a class the member function must be turned into a non member function, so define it as a friend function which gives it global scope.

Code: [Select]
class Stuff
{
public:
    Stuff   operator+(int nCount) { ... }
    friend
    Stuff & operator+(int nCount, const Stuff & stuff) { ... }
    friend
    Stuff & operator+(AcGePoint2d & pt, Stuff & stuff) { ... }
    const  Stuff & operator=(const Stuff &vec) { ... }
};

 Stuff st = Stuff() + 4;
 st = 4 + Stuff();
 st = AcGePoint2d(10,10) + Stuff();