bool myString::operator == ( const myString &r )
{
if ( size != r.size )
{
return false;
}
for ( int i = 0; i < size; i ++ )
{
if ( str [i] != r.str [i] )
{
return false;
}
}
return true;
}
bool myString::operator != ( const myString &r )
{
return ! ( this == &r );
}
int main ()
{
myString s1 ( "Happy" ), s2 ( " Birthday" ), s3 ( " to you Shafiq" ), s4, s5 ( "Happy" );
if ( s1 != s2 )
{
cout << s1.getStr () << " is not equal to " << s2.getStr () << endl;
}
else
{
cout << s1.getStr () << " is equal to " << s2.getStr () << endl;
}
if ( s1 != s5 )
{
cout << s1.getStr () << " is not equal to " << s5.getStr () << endl;
}
else
{
cout << s1.getStr () << " is equal to " << s5.getStr () << endl;
}
return 0;
}
The above program outputs like this ..
Happy is not equal to Birthday
Happy is not equal to Happy
Now If I just change the following peace of code
bool myString::operator != ( const myString &r )
{
return ! ( *this == r );
}
Results are shown as..
Happy is not equal to Birthday
Happy is equal to Happy
Why it happens, is there any difference between calling ( this == &rightObject ) and ( *this == rightObject ) ... of course there is a difference of syntax by is any difference semantically. Please tell thanks

New Topic/Question
Reply


MultiQuote




|