I have a class, and inside this class, a function that takes as argument another instance of the same class. Inside this function I need to do some stuff, but only if the argument passed is not the same instance as the current one. Is the following code the correct way to do this?
class Foo
{
void Foo::func(Foo &other)
{
if (this != &other)
// do stuff
}
}
Yes, (ptr == &ref)
is the correct way of determining whether the pointer and reference are to the same object. (Technically it might fail, if operator&
is overloaded, but that's a pretty unlikely situation, and one you'd obviously already be aware of, as the author of the class.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With