- More questions about operator overloading - 2 Updates
- Fixing some undefined behavior - 1 Update
- More questions about operator overloading - 1 Update
jacobnavia <jacob@jacob.remcomp.fr>: Jul 05 08:54PM +0200 Hi When doing operator overloading for some new type of number, there is a LOT of overloads to be done, one for each operator you want to overload, but also for each operation twice... For instance: T operator +(T arg1, double arg2); Then you handle T t; t = t+2.9; OK, but what about T operator +(double arg1, T arg2); That handles 2.9+t. Is that really necessary? Isn't addition commutative? No, since "abc" + "def" != "def" + "abc" That is why I have refrained in my implementation of operator overloading to make such an assumption. BUT Could it ever be that A != B and !(A == B) differ? I have decided no, so you do NOT have to overload != if you overload ==. Is that right? What do you think? Thanks in advance for your input. jacob |
Ian Collins <ian-news@hotmail.com>: Jul 06 08:34AM +1200 On 06/07/2020 06:54, jacobnavia wrote: > I have decided no, so you do NOT have to overload != if you overload ==. > Is that right? > What do you think? Most implementations I've seen of operator != are simply "return !operator ==" Maybe you should consider the spaceship operator? -- Ian. |
woodbrian77@gmail.com: Jul 05 12:57PM -0700 Shalom I have some undefined behavior in this program: https://github.com/Ebenezer-group/onwards/blob/master/src/cmw/tiers/cmwA.cc Basically I'm relying on some values being set after using placement new: req = &*pendingRequests.emplace_back(::new cmwRequest); frntBuf.getPacket((::sockaddr*)&req->frnt, &req->frntLn); gotAddr = true; ::new (req) cmwRequest(frntBuf); I need to be able to use "frnt" and "frntLn" after that last line. I guess the easiest way to handle it would be to pass the "frnt" and "frntLn" fields to the constructor I use with placement new. And then use them to initialize the fields. Thanks in advance. Brian Ebenenezer Enterprises - Enjoying programming again. https://webEbenezer.net |
ram@zedat.fu-berlin.de (Stefan Ram): Jul 05 07:55PM >but also for each operation twice... >For instance: >T operator +(T arg1, double arg2); You can also do overloading combined with implicit type conversion: main.cpp #include <iostream> #include <ostream> class my_type { double x; public: my_type( double const x = 0.0 ): x{ x } {} // converting constructor my_type & operator +=( const my_type & o ){ x += o.x; return *this; } ::std::ostream & print( ::std::ostream & stream )const { return stream << x; }}; inline my_type operator +( my_type result, my_type const & other ) { result += other; return result; } inline ::std::ostream& operator << ( ::std::ostream & stream, my_type const & object ) { return object.print( stream ); } int main() { auto const c = my_type{ 1.0 }; auto const d = c + 2.0; ::std::cout << d << '\n'; } transcript 3 . That's the reason why we do not want to have the operator + as a member function. Type conversion should work on either operand! But sometimes explicit conversions are prefered. In this case, one might have to write c + my_type{ 2.0 } , but this could even be praised as being more clear. |
You received this digest because you're subscribed to updates for this group. You can change your settings on the group membership page. To unsubscribe from this group and stop receiving emails from it send an email to comp.lang.c+++unsubscribe@googlegroups.com. |
No comments:
Post a Comment