3 Replies - 714 Views - Last Post: 17 August 2011 - 06:43 AM Rate Topic: -----

#1 shafiq.chaudhry.cs   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 43
  • Joined: 10-December 10

Difference between ( this == &rightObj ) and ( *this == rightObj )

Posted 17 August 2011 - 04:21 AM

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

Is This A Good Question/Topic? 0
  • +

Replies To: Difference between ( this == &rightObj ) and ( *this == rightObj )

#2 Oler1s   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1397
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Difference between ( this == &rightObj ) and ( *this == rightObj )

Posted 17 August 2011 - 04:24 AM

So..., what are the following:

- this
- &rightObject
- *this
- rightObject.
Was This Post Helpful? 1
  • +
  • -

#3 shafiq.chaudhry.cs   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 43
  • Joined: 10-December 10

Re: Difference between ( this == &rightObj ) and ( *this == rightObj )

Posted 17 August 2011 - 04:30 AM

hmmm I got the answer, I was comparing the addresses of two objects in
1 bool myString::operator != ( const myString &r )
2 {
3 return ! ( this == &r );
4 }


and definitely both objects are having different addresses in the memory space.

I must use this one ...
1	bool myString::operator != ( const myString &r )
2	{
3	    return ! ( *this == r );
4	}

Was This Post Helpful? 0
  • +
  • -

#4 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7507
  • View blog
  • Posts: 15,558
  • Joined: 16-October 07

Re: Difference between ( this == &rightObj ) and ( *this == rightObj )

Posted 17 August 2011 - 06:43 AM

When in doubt, test.

Here's a quick program:
#include<iostream>

using namespace std;

class Foo {
private:
	int n;
public:
	Foo(int _n) : n(_n) { }
	void showCompare(const Foo &other) {
		cout << "this: "; print();
		cout << "other: "; other.print();
		cout << "this==&other: " << (this==&other) << endl;
		cout << "*this==other: " << (*this==other) << endl;
		cout << endl;
	}
	bool operator==(const Foo &other) const { cout << "oper called" << endl; return n==other.n; }
	void print() const { cout << "Foo(" << n << ") @" << (unsigned long)this << endl; }
};


int main() {
	Foo x(1), y(2), z(1);
	x.showCompare(x);
	x.showCompare(y);
	x.showCompare(z);
	return 0;
}



Results:
this: Foo(1) @140734662243172
other: Foo(1) @140734662243172
this==&other: 1
oper called
*this==other: 1

this: Foo(1) @140734662243172
other: Foo(2) @140734662243176
this==&other: 0
oper called
*this==other: 0

this: Foo(1) @140734662243172
other: Foo(1) @140734662243180
this==&other: 0
oper called
*this==other: 1



Hope this helps.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1