I want to overload the bitwise & for a class, NOT the addressof &. How do i make sure I'm overriding the right one?
I want to overload the bitwise & for a class, NOT the addressof &. How do i make sure I'm overriding the right one?
Example
Help?Code:int MyClass::operator &(const MyClass &other) { return this->value & other.value; }
It would have no parameters as a member function, or one parameter as a free function.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
The "address of" operator would be the one that takes no arguments...
Soma
1 and 3 are address-of operators, 2 and 4 are bitwise and.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; }
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"
More accurately:
There should also be a const overload for the first one, methinks: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; }
EDIT:Code:const int* MyClass::operator &() const { return &value; }
Ah, then that means that the third one should be:
with a possible non-const overload:Code:friend const int* operator &(const 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.Code:friend int* operator &(MyClass &a) { return &a.value; }
Last edited by laserlight; 04-24-2008 at 01:53 AM.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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
For a home brewed bignum library?I have overloaded bitwise and
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".but overloading address-of is evil.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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
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"