#include <iostream>
using namespace std;
class Dummy {
public:
bool isitme (Dummy& param);
};
bool Dummy::isitme (Dummy& param)
{
if (¶m == this) return true;
else return false;
}
int main () {
Dummy a;
Dummy* b = &a;
if ( b->isitme(a) )
cout << "yes, &a is b\n";
return 0;
}
Regarding the code above, in 'isitme' function, the if condition:
if (¶m == this)
Shouldn't be:
if (param == this)
as 'this' is a pointer, and 'param' is a pointer too, so if we said:
if (¶m == this)
that would be a comparison between an address of a pointer (¶m) and a pointer (this), which is not what we are looking for; checking if a parameter passed to a member function is the object itself?
A reference type doesn't have the same semantics with a pointer type. Its unspecified how compilers implement references. A reference type doesn't have an address per say. When you take the address of a reference type the result will always be the address of the object it was bound to.
So the code below tests the original object's address param
binded to with this
;
bool Dummy::isitme (Dummy& param)
{
if (¶m == this) return true;
else return false;
}
In the case of pointers, just test the pointers for equality.
bool Dummy::isitme (Dummy* param)
{
if (param == this) return true;
else return false;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.