What if i overload the != operator and use class!= inside one of other operator overloaders, does it accept it as non-overloaded or overloaded? I am trying to create a noob_ptr (a kind of custom-pointer wraper i am thinking of)
class noob_ptr
{
private: //does this change the behaviour? public? protected?
bool operator!=(noob_ptr x)
{
...
}
bool operator,(noob_ptr y)
{
...
if(y!=z)...
...
}
...
}
Does below example cancel usage of overloaded-operator in my class?
class noob_ptr
{
protected: //or public
bool operator,(noob_ptr y) //yes, z is also a noob_ptr
{
...
if(y!=z)...
...
}
...
private:
bool operator!=(noob_ptr x)
{
...
}
}
If type of
zis also anoob_ptr, then the answer is YES, it will call the overloadedoperator !=for your class.Also, I’d suggest you this comparison method signature:
so it can be used for constant pointers and also avoid object copying while calling the overloaded operator.
UPD: If you declare
operator !=as private, then it’ll be available in all member functions ofnoob_ptrclass, friend classes and functions ofnoob_ptrclass, and for all other usages will result in a compilation error with a message like: “operator != is inaccessible due to its protection level”