10

d1 + 4有效,但4 + d1即使 4 可以隐式转换为 GMan 也无效。为什么它们不相等?

struct GMan
{
    int a, b;

    GMan() : a(), b() {}
    GMan(int _a) : a(_a), b() {}
    GMan(int _a, int _b) : a(_a), b(_b) {}

    GMan operator +(const GMan& _b)
    {
         GMan d;
         d.a = this->a + _b.a;
         d.b = this->b + _b.b;
         return d;
    }
};

int main()
{
    GMan d1(1, 2), d(2);
    GMan d3;
    d3 = d1 + 4; 
    d3 = 4 + d1;
}
4

2 回答 2

12

C++ 编译器将调用x + y转换为以下两种调用之一(取决于是否x属于类类型,以及是否存在这样的函数):

  1. 成员函数

    x.operator +(y);
    
  2. 自由功能

    operator +(x, y);
    

现在 C++ 有一个简单的规则:在成员访问运算符 ( ) 之前不能发生隐式转换.。这样,x在上面的代码中,第一个代码中不能进行隐式转换,但可以在第二个代码中进行。

这条规则是有道理的:如果x可以在上面的第一个代码中隐式转换,C++ 编译器将不再知道要调用哪个函数(即它属于哪个类),因此它必须在所有现有类中搜索匹配的成员功能。这将对 C++ 的类型系统造成严重破坏,并使重载规则更加复杂和混乱。

于 2010-11-12T13:33:01.887 回答
3

这个答案是正确的。然后,这些要点需要实现此类运算符的规范方式:

struct GMan
{
    int a, b;

    /* Side-note: these could be combined:
    GMan():a(),b(){}
    GMan(int _a):a(_a),b(){}
    GMan(int _a, int _b):a(_a),b(_b){}
    */
    GMan(int _a = 0, int _b = 0) : a(_a), b(_b){} // into this

    // first implement the mutating operator
    GMan& operator+=(const GMan& _b)
    {
        // the use of 'this' to access members
        // is generally seen as noise
        a += _b.a;
        b += _b.b;

        return *this;
    }
};

// then use it to implement the non-mutating operator, as a free-function
// (always prefer free-functions over member-functions, for various reasons)
GMan operator+(GMan _a, const GMan& _b)
{
    _a += b; // code re-use
    return _a;
}

其他运营商依此类推。

于 2010-11-12T14:32:45.153 回答