operator != not automatically defined when operator == is defined

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Observe:

class MyClass
{
public :
    MyClass ( int x ) : i ( x ) {}
    bool operator == ( const MyClass & other )
        { return i == other . i ; }
private :
    int i ;
} ;
int main ( void )
{
    MyClass a ( 1 ), b ( 2 ) ;
    if ( a != b ) {}
}

Trying to compile this gives:

operator-notequal.cpp:14: error: no match for ‘operator!=’ in ‘a != b’

Now there need conceivably be no definition for operator != other than the not-ted value of what operator == gives, so why does gcc want me to define operator != separately?

Similarly even if I define only operator !=, gcc will tell me to separately define operator == if I want to use that.

For the pairs:

== and !=
> and <=
< and >=

it is mathematically illogical to have any definition for each operator in the pair which does not output same as the not-ted output of the other operator in the pair, but gcc will not allow me to use an operator unless it has been explicitly defined even if its logical negative operator has been defined. I believe this is a defect in gcc.

Your comments?

Shriramana Sharma.

-
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [Linux Assembler]     [Git]     [Kernel List]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [C Programming]     [Yosemite Campsites]     [Yosemite News]     [GCC Help]

  Powered by Linux