1

I am trying to overload the = operator on a simple C++ class called Set that contains a dynamic array of ints. For the = operator, I first want to check for self assignment, so I wanted to compare 2 pointers to make see if they have the same memory address. Here's the code:

Set& Set::operator=(const Set& setEqual)
{
//first check for self assignment
if(setEqual == this*)
    cout << "this is self assignment";
}

The error spat out is error: expected primary-expression before ')' token

I believe I'm having a misunderstanding of pointers again, so if anyone could point (ha!) me in the right direction, I'd appreciate it.

Ross Hettel
  • 1,039
  • 1
  • 9
  • 19
  • 2
    If you use [the copy-and-swap idiom](http://stackoverflow.com/questions/3279543/what-is-the-copy-and-swap-idiom) you don't need the self assignment test (and you get lots of other benefits). – James McNellis Oct 20 '10 at 05:49
  • 2
    4th line: To dereference a pointer you put * before the variable name, so it's `*variableName`, not `variableName*` – Warty Oct 20 '10 at 05:50
  • "This*" just reminds of "This is star plus, and you are watching star movies." No offense only fun. – Manoj R Oct 20 '10 at 05:55
  • Use the copy-and-swap idiom, you're off track trying to do it this way. – GManNickG Oct 20 '10 at 06:13
  • I will definitely look into the copy and swap idiom, didn't know that took care of everything. Thanks for the link! – Ross Hettel Oct 20 '10 at 06:35

3 Answers3

6

The error is becuase this* is not valid - * is either infix (in multiplication) or prefix (in dereferencing pointers).

You probably want &setEqual == this - that is assigning from an object at the same memory address or setEqual==*this - comparing equality using whatever operator== you have defined

Martin York
  • 257,169
  • 86
  • 333
  • 562
tobyodavies
  • 27,347
  • 5
  • 42
  • 57
  • No, it would be silly to compare the sets. To avoid self-assignment he wants to compare pointers to the set objects, i.e. `this == &setEqual`. But as remarked for the question, he'd be better off using the copy-and-swap idiom (in particular for exception safety). – Cheers and hth. - Alf Oct 20 '10 at 05:54
  • I didn't even read what he was comparing, i just saw the syntax error and then gave the two possible (syntactically) correct ways of comparing equality. regardless, i feel its better to be complete - maybe he does want them compared as equal if both sets contain the name ethel, he didn't say! – tobyodavies Oct 20 '10 at 05:58
  • Hey thanks, that worked out. I'm still iffy on pointer symantics. That's a good explanation on where I should put the * and why. – Ross Hettel Oct 20 '10 at 06:34
4

If you want to compare the address of the thing pointed too you really want this:

Set& Set::operator=(const Set& setEqual)
{
//first check for self assignment
if(&setEqual == this)
    cout << "this is self assignment";
}

Using (setEqual==*this) as sugested by some of the solutions compares if the objects are equal under operator==.

Michael Anderson
  • 70,661
  • 7
  • 134
  • 187
4

To detect self-assignment you need

if(&setEqual == this)

you should never use

if(setEqual == *this)

for detecting self-assignment as the latter statement will invoke comparison of the objects, which might be overloaded in the way you don't expect and is likely slower as well.

sharptooth
  • 167,383
  • 100
  • 513
  • 979
  • Thanks, that helps. I am overloading the ++ operator in this class too, so I will definitely do it the first way. – Ross Hettel Oct 20 '10 at 06:33