Overload operator !

How can i overload the ! operator?
Do you have an example or any references for operators overload?
Thanks.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>

int main()
{
	class A
	{
	public:
		A( int i = 0 ) : x( i ) {}
		bool operator !() const { return ( x % 2 == 0 ); }
	private:
		int x;
	} a1( 10 ), a2( 9 );

	if ( !a1 ) std::cout << "a1 is even\n";
	else std::cout << "a1 is odd\n";

	if ( !a2 ) std::cout << "a2 is even\n";
	else std::cout << "a2 is odd\n";
}
Which operator do you want to overload ?
please google it .
closed account (zb0S216C)
The only rule for overloading the logical negation operator is that the operator must not accept arguments, not even default arguments.

You can be an outlaw, though:

1
2
3
4
5
struct Object
{
    const char *operator ! () { return("Not Equal"); }  // Don't do this.
    bool const operator ! () const { return(...); } // Do this instead.
};

My recommendation to you: Don't be an outlaw; I'm just showing you what not to do.

Wazzak
Topic archived. No new replies allowed.