Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Oct 2002
    Location
    middle of nowhere
    Posts
    34
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Overloading Unary Operators?

    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
    Last edited by Shawn Curry; 10-14-2002 at 07:36 PM.

  • #2
    New Coder
    Join Date
    Oct 2002
    Location
    middle of nowhere
    Posts
    34
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •