...

Overloading Unary Operators?

Shawn Curry
10-14-2002, 07:33 PM
The books that I have on C++ dont really explain the difference in overloading binary and unary operators. I have a long math class to handle extremely large numbers. I overloaded pretty much all the binary operators for it(+, +=, -, -= etc) but it would be great if I could overload ++ and ! (for factorials). I tried to look it up on the net and it says to do it just like I overloaded the binaries. Is that right? and if so, what do I have to do differently?

class xNumber
{
public:

char* number;

public:

xNumber operator+(const xNumber&);
xNumber& operator+=(const xNumber&);

(...)

};

xNumber xNumber:: operator+(const xNumber& xn)
{
(...)

}

xNumber& xNumber:: operator+=(const xNumber& xn)
{
(...)

}

That's what my binary ops look like. Is it just in the way I handle it inside the function or what?

Thanks

Shawn Curry
10-15-2002, 04:46 AM
Well, just in case your reading this and dont know:

class first
{

char* string;

public:

first operator+(const first& f);// binary

void operator++()// unary prefix
void operator++(int)// unary postfix

};

//outside the class

bool operator>(const first& f, const first& s)//binary(two arguements)
{
(...)
}

im still not sure how to implement a unary op outside of the class, but I cant really see why you would want to.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum