Hello, first of all, my best regards for the developers of gcc, it is a very big and great piece of work, I appreciate it much and use it frequently.
I am very curios over a piece of code I wrote recently. In a slight variation it produces a compile error from g++ and I have no idea if i) I do not understand c++ enough ii) it is a bug in g++ iii) it is a design flaw in c++ so I decided to share this one with you (you can freely ignore it without offending me). Topic: overloading the == and != operators: this code #include <iostream> // comment in to get compile error #define BUG struct t_settings { bool operator==(t_settings const &a){ if (a.x != this->x) { return (false); } if (a.y != this->y) { return (false); } if (a.z != this->z) { return (false); } return (true); } #ifndef BUG bool operator!=(t_settings const &a){ return ( ! ( *this == a ) ); } #else bool operator!=(t_settings const &a){ return ( ! ( a == *this ) ); } #endif int x; int y; int z; }; int main (int argc, char **argv){ t_settings mySettings; mySettings.x=1; mySettings.y=2; mySettings.z=3; t_settings hisSettings; hisSettings.x=1; hisSettings.y=2; hisSettings.z=4; if (mySettings != hisSettings){ std::cout << "Settings not equal" << std::endl; } else { std::cout << "Settings equal" << std::endl; } return (0); } g++ (version 3 and 4 equally) produces: In member function `bool t_settings::operator!=(const t_settings&)': error: passing `const t_settings' as `this' argument of `bool t_settings::operator==(const t_settings&)' discards qualifiers comment out #define BUG and it compiles. So the order of a and *this are all that matters. Sorry, but I have no idea why this is so. Greetings Bernhard Günther, Bonn, Germany -- -------------------------------------------------------------------------- Bernhard Guenther <[EMAIL PROTECTED]> <[EMAIL PROTECTED]> ICQ: 1632197, Jabber: [EMAIL PROTECTED] Tel: +4922896779696, Cell: +491704931668 --------------------------------------------------------------------------
pgpWgssbsthSC.pgp
Description: PGP signature