c++ - Checking if the object passed to a method is "this" -
i'm trying solve following problem:
suppose i'm writing class has method mymethod modifies both *this , argument passed:
class myclass { //some code here void mymethod(myclass& other) { //modify *this , other } }; the problem want method nothing when following piece called:
myclass x; x.mymethod(x); checking equality not enough, because want able call 2 identical objects.
in more down-to-earth way, example, suppose myclass std::set , mymethod merges 2 sets, emptying other. 2 identical sets can merged, can't empty , fill 1 set @ same time.
how can check this? advice appreciated.
you can compare address of other this:
class myclass { //some code here void mymethod(myclass& other) { if (this != &other) { //modify *this , other } } }; since pass reference, pointers equal if pass same object function 1 called on.
Comments
Post a Comment