Problem with overloading an operator.

This is a discussion on Problem with overloading an operator. within the C++ Programming forums, part of the General Programming Boards category; Hello, I have to overload "<" and ">" operators. So I do: Code: bool bignum::operator<(const bignum& a) { // here ...

  1. #1
    Registered User
    Join Date
    May 2005
    Posts
    76

    Problem with overloading an operator.

    Hello,
    I have to overload "<" and ">" operators. So I do:
    Code:
    bool bignum::operator<(const bignum& a)
    {
            // here is some code comparing *this and a.
    }
    now I want to use operator "<" to overload ">":
    Code:
    bool bignum::operator>(const bignum& a)
    {
            return  a < *this;
    }
    and I get a compile error:
    main.cpp: In member function `bool bignum::operator>(const bignum&)':
    main.cpp:157: error: passing `const bignum' as `this' argument of `bool bignum::operator<(const bignum&)' discards qualifiers
    Can anyone help me ?
    --
    Regards,
    apacz

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,414
    I suggest making them friend functions, and then writing:

    Code:
    bool operator<(const bignum& lhs, const bignum& rhs)
    {
    	// here is some code comparing lhs and rhs.
    }
    
    bool operator>(const bignum& lhs, const bignum& rhs)
    {
    	return rhs < lhs;
    Making them friend functions means that expressions like (1 < x) will be allowed, assuming that int is convertible to bignum (e.g. via a non-explicit constructor that accepts an int).
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    May 2005
    Posts
    76
    Thanks. But there is no way to make this overload look like: bool operator<(const bignum& rhs) ? Because your overload takes two arguments - what's the real difference between those two versions ?
    --
    Regards,
    apacz

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,414
    But there is no way to make this overload look like: bool operator<(const bignum& rhs) ?
    Yes, there is. Make your member function versions const.

    Because your overload takes two arguments - what's the real difference between those two versions ?
    The useful difference is what I mentioned: the friend function version is more flexible than the member function version. Actually, since operator> is implemented in terms of operator< you do not even need to make it a friend function.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. operator overloading slight problem
    By reddzer in forum C++ Programming
    Replies: 2
    Last Post: 02-23-2006, 03:30 PM
  2. oops? not sure. operator overloading (possible) problem
    By w00tw00tkab00t in forum C++ Programming
    Replies: 8
    Last Post: 02-08-2006, 04:38 AM
  3. bug in my operator += overloading method?
    By registering in forum C++ Programming
    Replies: 4
    Last Post: 10-29-2003, 10:05 AM
  4. Full operator overloading in BCB5
    By Mario in forum C++ Programming
    Replies: 5
    Last Post: 06-01-2002, 02:15 PM
  5. operator overloading
    By ihsir in forum C++ Programming
    Replies: 4
    Last Post: 02-09-2002, 03:38 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21