1

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.

Manu343726
  • 13,969
  • 4
  • 40
  • 75
  • That's not a const reference in the sense you're thinking of. It's a reference to a pointer to a `const T`. – chris Feb 27 '14 at 22:06
  • I must be missing something, if it T was const it would be written as T * const right? – Harley Armstrong Feb 27 '14 at 22:15
  • const T* is a pointer to a const T object T* const is a constant pointer to a non-constant T object const T* const is a const pointer to a const T object T const* and const T* are equivalent. – Jens Feb 27 '14 at 22:24
  • I think I see the const problem, I have updated the question in the ALSO section based on some new issues that have arisen. – Harley Armstrong Feb 27 '14 at 22:39
  • Be careful while tagging: C++ templates are not the same (so far) as Java generics (A Turing Complete system vs some form of syntactic sugar :P) – Manu343726 Feb 27 '14 at 23:15
  • Why are you using pointers at all? – Manu343726 Feb 27 '14 at 23:18
  • @Manu343726 Thank you for the correction, some times these terms can be very confusing. To be honest, this japan class has had a lot of things removed from it. It is mainly used for scoping pointer to be safe when working with C spacial data libraries. – Harley Armstrong Feb 28 '14 at 00:55

1 Answers1

1

In C++, 0 is an abused integer. It is used to reflect, well, 0 as a value of type int and it is also used to represent a null pointer.

Your types are fundamentally different. There's no const correctness issue, it's just that NULL, is a macro for 0 and that's something that doesn't cooperate well with generic programming. You could try

static_cast<T*>(NULL); 

when comparing with null pointer, or better yet use nullptr, which is what the new standard wants you to be using when you mean "a null pointer"

Community
  • 1
  • 1
Nikos Athanasiou
  • 29,616
  • 15
  • 87
  • 153