I have implemented a custom pointer class, lets call it japan. Here is a declaration and definition, as well as a main function I am trying to work with.
class japan
{
public:
japan(T *ptr) : ptr(ptr)
{}
T *get()
{
return this->ptr;
}
bool operator==(const T *&other)
{
return ( this->get() == other );
}
friend bool operator==(const T *&l, japan<T> r)
{
return ( r == l );
}
private:
T *ptr;
};
int main(void)
{
int j;
int *k = & j;
japan<int> nihon(k);
if(k == nihon)
if(nihon == k)
std::cout << "yay\n";
if(nihon == NULL)
return 1;
else
return 0;
if(NULL == nihon)
return 5;
}
My == operator seem to working fine on actual T *s when they are passed in, however if I evoke it with NULL my compiler produces the errors:
stuff.cpp:40:15: error: no match for 'operator==' in '0l == nihon'
stuff.cpp:40:15: note: candidate is:
stuff.cpp:21:16: note: bool operator==(const int*&, japan<int>)
stuff.cpp:21:16: note: no known conversion for argument 1 from 'long int' to 'const int*&'
From what I understand... there should be some form of conversion from T * to long int, or NULL's assignment operator would not function. I then reworked my operator a little.
Both of these functions work
friend bool operator==(T *l, japan<T> r);
friend bool operator==(const T *l, japan<T> r);
However This one fails.
friend bool operator==(const T *&l, japan<T> r);
This makes scene to me, changing NULL would change a value that is never used, but it is a const reference... so why does this fail. Is it common in c++ to not use references on boolean operators? Most of the examples I see always use a reference, but this seems like this would only work for lvalues... So is it better practice to pass by value while overloading?
ALSO:
Why would I get the warning :
warning: ISO C++ says that these are ambiguous, even though the worst conversion for the first is better than the worst conversion for the second: [enabled by default]
ptr.h:106:9: note: candidate 1: bool japan<T>::operator==(const T* const&) const [with T = Geometry]
stuff.cpp:239:15: note: candidate 2: operator==(int, long int) <built-in>
Is my japan class somehow exposing its self as a T when == is used, I ask this because I found this class in something I was working on with no == operator defined (despite it being used in many places), and I added one... only to find no end of problems.