user13659234
user13659234

Reputation:

C++, = operator different options

I Wrote the following function for = operator:

Set& Set::operator=(const Set& s)
{
    delete[] data;
    data=new int [s.size];
    size=s.size;
    maxSize=s.size;
    for (int i=0;i<size;i++)
    {
        data[i]=s.data[i];
    }
    return *this;
}

But, I wanted the following code to work as expected (without deleting current data):

Set s1,s2;
s1=s1;

So I made the following changes:

Set& Set::operator=(const Set& s)
{
    if (this == &s)
    {
        return *this;
    }
    delete[] data;
    data=new int [s.size];
    size=s.size;
    maxSize=s.size;
    for (int i=0;i<size;i++)
    {
        data[i]=s.data[i];
    }
    return *this;
}

my question is:

1. Why this is correct:

    if (this == &s)
    {
        return *this;
    }

while this is not:

    if (this == s)
    {
        return *this;
    }

s is a reference so it's a location on memory and there is no need to use &operator with it

2. Can we write the following instead of what is shown above:

    if (*this == s)
    {
        return *this;
    }

Upvotes: 0

Views: 67

Answers (4)

rekkalmd
rekkalmd

Reputation: 181

If I understand exactly what you're searching about,

   if (this != &s)
    {
       //Processing 
    }

In this case you're telling the Compiler, if this address (of the current object) isn't the same as the address of the target (s), you can process the implementation.

Taking as example, two characters from a game in battle, if we're on a

void Character::punch(Character &other); //function

(this == &s) Means that the Subject is punching himself (not what we're expecting), for a safe implementation :

void Character::punch(Character &other) {
           if (this != &s)
            {
               other.recievesDamage(punchDmg); /* w/ punchDmg defined */
            }
 } 

So it depends on your thoughts, and what you want to do, most importantly, it means to compare if the Current Object is the same as the other one (s in your example), if they have not the same reference, despite same attributes, that means that they are just similar on their data, but they aren't the same one.

if (*this == s)
        {
           //Processing 
        }

You are just comparing between s and the object.

 if (this == s)
            {
               //Processing 
            }

you're comparing between a pointer reference and an object.

Upvotes: 0

Jesper Juhl
Jesper Juhl

Reputation: 31467

s is a reference to an object. Think of it as an alias. It is not a pointer, So you cannot compare it to this which is a pointer.

As for comparing *this to s. That doesn't quite do what you want. It compares the two objects for equality and many objects of the type may contain the same values and thus compare equal. But, what you want is not whether the objects compare equal, you want to know if they are the same object.

Comparing &s with this tells you if the two objects are located at the same memory address or not. If they are at the same location then they are the same object, if not then they are just two instances (possibly equal) of the same type. So this is what you want.

Upvotes: 2

Jeffrey
Jeffrey

Reputation: 11400

s is a reference so it's a location on memory and there is no need to use &operator with it

Yes, s is a reference. But the fact that it is a location in memory is an implementation detail. Syntactically, it behaves like an object. You need to use & in front of it to get a usable address (although, yeah, obviously behind the scene the address is already there).

if (*this == s)

No, you shouldn't use this. Your operator== will compare whether two list are the same (same content) not whether they are the same list (same object). The reason for avoiding the copy is because the object would be modifying itself.

But, if your operator== is well defined, you'll be able to get away with this. When you assign two identical objects, it will simply do nothing. I'd advise against. You never know, in the future what subtleties there will be in comparing objects when they get more complex.

Upvotes: 1

cigien
cigien

Reputation: 60218

this is a pointer that stores the address of an object, and s is a reference to some object.

This check:

if (this == &s)  // compares addresses

is checking if the address of s is the same as the address of the object being assigned to. This is what you want, and is correct.

This check:

if (this == s)  // error, good

compares a pointer and an object, which will give a compiler error.

This check:

if (*this == s)  // compares values

compares the value of s with the object. This is incorrect, since 2 distinct objects can have the same value, while of course they have different addresses. This is not a case of self-assignment, and is incorrect.

Upvotes: 1

Related Questions