How overload operator & (bitwise) versus & (address)?

This is a discussion on How overload operator & (bitwise) versus & (address)? within the C++ Programming forums, part of the General Programming Boards category; I want to overload the bitwise & for a class, NOT the addressof &. How do i make sure I'm ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    184

    How overload operator & (bitwise) versus & (address)?

    I want to overload the bitwise & for a class, NOT the addressof &. How do i make sure I'm overriding the right one?

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Example
    Code:
    int MyClass::operator &(const MyClass &other)
    {
       return this->value & other.value;
    }
    Help?

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    184
    Quote Originally Posted by master5001 View Post
    Example
    Code:
    int MyClass::operator &(const MyClass &other)
    {
       return this->value & other.value;
    }
    Help?
    Yes, thanks! So what would the address-of operator overloading look like? (Just curious)

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,968
    It would have no parameters as a member function, or one parameter as a free 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

  5. #5
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    3,798
    The "address of" operator would be the one that takes no arguments...

    Soma

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,261
    Code:
    int* MyClass::operator &()
    {
       return &value;
    }
    int MyClass::operator &(const MyClass &a)
    {
       return value & a.value;
    }
    friend int* MyClass::operator &(const MyClass &a)
    {
       return &a.value;
    }
    friend int MyClass::operator &(const MyClass &a, const MyClass &b)
    {
       return a.value & b.value;
    }
    1 and 3 are address-of operators, 2 and 4 are bitwise and.
    I'd recommend using 1 and 4 from the above.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,968
    More accurately:
    Code:
    int* MyClass::operator &()
    {
       return &value;
    }
    int MyClass::operator &(const MyClass &a) const
    {
       return value & a.value;
    }
    friend int* operator &(const MyClass &a)
    {
       return &a.value;
    }
    friend int operator &(const MyClass &a, const MyClass &b)
    {
       return a.value & b.value;
    }
    There should also be a const overload for the first one, methinks:
    Code:
    const int* MyClass::operator &() const
    {
       return &value;
    }
    EDIT:
    Ah, then that means that the third one should be:
    Code:
    friend const int* operator &(const MyClass &a)
    {
       return &a.value;
    }
    with a possible non-const overload:
    Code:
    friend int* operator &(MyClass &a)
    {
       return &a.value;
    }
    But I personally have never actually overloaded the address of operator, or the bitwise and operator for that matter, in my own code.
    Last edited by laserlight; 04-24-2008 at 01:53 AM.
    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

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    I have overloaded bitwise and, but overloading address-of is evil.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,968
    I have overloaded bitwise and
    For a home brewed bignum library?

    but overloading address-of is evil.
    Yeah, I had to check that it was not one of the operators for which overloading was forbidden before I dared to state that it "would have no parameters as a member function, or one parameter as a free 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

  10. #10
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    Quote Originally Posted by laserlight View Post
    For a home brewed bignum library?
    No, I believe it was for a meta-programming experiment, but I can't remember it well.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  11. #11
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,261
    Quote Originally Posted by laserlight View Post
    For a home brewed bignum library?


    Yeah, I had to check that it was not one of the operators for which overloading was forbidden before I dared to state that it "would have no parameters as a member function, or one parameter as a free function".
    Yeah thanks for correcting the above, I knew I would have forgotten something, I was in a rush.

    CComPtr overloads the address-of operator, then in order to use CComPtr in std containers you need to use CAdapt to effectively stop the overloading again. Translation, it certainly can be evil.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What does this do (Windows API)?
    By EVOEx in forum Windows Programming
    Replies: 4
    Last Post: 12-19-2008, 09:48 AM
  2. Writing array, to file
    By zootreeves in forum C Programming
    Replies: 9
    Last Post: 09-08-2007, 05:06 PM
  3. I thought pointers were pointers...
    By keira in forum C Programming
    Replies: 19
    Last Post: 08-15-2007, 11:48 PM
  4. DX - CreateDevice - D3DERR_INVALIDCALL
    By Tonto in forum Game Programming
    Replies: 3
    Last Post: 12-01-2006, 06:17 PM
  5. Im so lost at . .
    By hermit in forum C Programming
    Replies: 18
    Last Post: 05-15-2002, 01:26 AM

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