Why should I overload a C++ operator as a global function (STL does) and what are the caveats?

拜拜、爱过 提交于 2019-12-18 08:54:20

问题


Why would I want to overload a C++ operator() as global and not member function. For example, the == operator.

Why is this done? for example in STL libraries.


回答1:


The usual rule is for operators which modify the left hand object to be members, and binary operators which return a new object to be free functions; the main motivation for the latter is because the compiler will not convert the left hand side to match a member; if your class supports any implicit conversions, then all of the usual binary operators should be free functions, so that the same rules of conversion apply
for the left hand side and the right hand side, e.g.:

class Complex
{
public:
    Complex(double r, double i = 0.0);
    bool operator==( Complex const& other ) const;
};

Complex a;
//  ...
if ( a == 1.0 ) // OK
//  ...
if ( 1.0 == a ) // error.

but:

class Complex
{
public:
    Complex(double r, double i = 0.0);
    friend bool operator==( Complex const& lhs, Complex const& rhs ) const;
};

Complex a;
//  ...
if ( a == 1.0 ) // OK
//  ...
if ( 1.0 == a ) // OK

One elegant way of achieving this is to define the basic operations in terms of member functions—for things like + or -, these would be operator+= and operator-=; for comparison, you'd need to define an arbitrary conventions, a member isEqual, or compare (which would return <, == or > zero according to the results, then inherit from a template along the lines of:

template <typename T>
class ArithmeticOperators
{
    friend T operator+( T const& lhs, T const& rhs )
    {
        T result( lhs );
        result += rhs;
        return result;
    }
    //  And so on, for all of the binary operators.
};

class Complex : public ArithmeticOperators<Complex>
{
public:
    //  ...
    Complex& operator+=( Complex const& other );
    //  etc.
};

Note that there is some argument for making the operator <op>= functions free functions as well: the fact that the free function will take a non-const reference as its first argument, and thus requires an lvalue (like the built-in operator <op>=). This doesn't seem to be the usual practice, however, probably because operator= must be a member, and it seems more natural to treat the operator <op>= in the same mannter.




回答2:


If I remember correctly, operator = must be a member function. Regarding operator ==, I figure you don't actually mean global but free function instead (STL does not define operators globally). There are a couple of things to consider, one is decoupling from the class itself: If your operator can be defined in terms of the public interface of your class, then you are better off implementing it that way to keep the access to the implementation internals to the bare minimum. The other fundamental advantage is the possibility to implemement an operator where your type comes as the second operand, consider equality between types T and U:

bool operator ==( T const& t, U const& u ){ ... }
bool operator ==( U const& t, T const& u ){ ... }

If objects of type T and U can be equally compared, then it makes sense that both t == u and u == t are valid and both yield the same result. If you were defining this operator as a member function, then one would be within the implementation of T and the other within the implementation of U. Now consider U is a 3rd party type outside your control, or even better is a fundamental type such as int, now there is no other way for you to provide such operator but to provide the free function version of it.




回答3:


The rules for matching a member function against an argument are different from the rules for matching the first argument of a free function. I'm not certain exactly how they are different, but since they are different it's actually preferable to implement most binary operators as free functions instead of member functions in order that argument matching operates symmetrically for both arguments.

There is an except for postfix ++, but that's because it isn't really a binary operator, and only plays one when you overload it in order to have a way of distinguishing between it and prefix ++.



来源:https://stackoverflow.com/questions/7578682/why-should-i-overload-a-c-operator-as-a-global-function-stl-does-and-what-ar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!