When to pass by reference for overloading operators in c++?

当经过参考重载操作符在c++中吗?

问题 (Question)

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.

我实现了一个自定义指针类,可以称之为日本。这是一个声明和定义,以及主要功能我努力工作。

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;
  }

我= =操作符似乎工作好实际T *年代传入时,然而如果我唤起空我的编译器产生的错误:

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*&'

据我所知……应该有某种形式的转换从T *长int,或空的赋值运算符不会功能。然后我修改了我的运营商。

这两种功能的工作

friend bool operator==(T *l, japan<T> r);
friend bool operator==(const T *l, japan<T> r);

然而这一失败。

friend bool operator==(const T *&l, japan<T> r);

这使得场景对我来说,改变从未使用NULL值会改变,但它是一个常量引用…那么为什么这个失败。它是常见的在c++中不使用布尔操作符引用?大部分的例子我看到总是使用一个引用,但是这似乎只会为左值工作……所以它是更好的实践经过价值而重载?

另外:

为什么我得到的警告:

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>

是我的日本类暴露其自我T = =使用时,我问这个,因为我发现这个类在我正没有= =操作符定义(尽管它被使用在很多地方),我添加一个……却发现没有问题。

最佳答案 (Best Answer)

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"

在c++中,0是一个滥用整数。它是用来反映,0作为一个int类型的价值也是用来表示一个空指针。

你的类型是完全不同的。没有常量的正确性问题,只是空,是0,这是一个宏和泛型编程不配合得很好。你可以试试

static_cast<T*>(NULL); 

本文翻译自StackoverFlow,英语好的童鞋可直接参考原文:http://stackoverflow.com/questions/22081515