If you've worked as a C++ programmer for long enough, I'm sure you have plenty of stories about operator overloading abuse. Today, Jake Wyland shares the worst abuse he's seen to date ...
"The company I used to work for went on a hiring binge to staff up for a big project. The work required a lot of domain experience working with a gargantuan plug-in API. To make learning the API easier, we decided to have weekly code excursions where people would come in and present the code they were working on and show off how to use the portion of the API they were working on. During one such code excursion, I saw the following code scroll by in the image projected on the wall:
bool fixNodes(State &s) { CFNode *firstPtr = s.getFirstNode(); bool done = false; while(!done) { for (CFNode* nptr = *firstPtr + 1; *nptr + 1; nptr = *nptr + 1) { // Some code ... } // ... more code } }"Knowing that almost every pointer value plus 1 will result in a non-NULL pointer, I said that the code contained a bug because the for-loop would never terminate.
"Undaunted, the young coder doing the presentation said that is surely would terminate as he had overloaded the '+' operator for CFNode. He then showed us the overload in the header file:
class CFNode { Point3D nodePoint; CFNode* prevNode; CFNode* nextNode; public: CFNode* getNextNode(); CFNode* getPrevNode(); CFNode* firstNode(); CFNode* lastNode(); // More fields deleted ~CFNode(); CFNode(Point3D point, CFNode* const prevNode); // Returns the same thing as getNextNode() CFNode* operator+(const int rhs); void takeAction(); // More methods deleted };"After that presentation, operator overloading was banned.