繁体   English   中英

二进制运算符+,= const重载

[英]binary operator + , = overloading with const

我正在学习C ++概念,希望对解释有所帮助。 为什么语句4不编译而语句1为什么编译。

class X {
    public:
            X& operator= (const X& rhs);
            const X& operator+ (const X& rhs) const;
            const X& operator+ (int m);
    private:
            int n;
};

int main() {
        X a, b, c;
        a = a + 5 + c; //statement 1 - No compiler errors
        a = b + 5; //statement 2
        a = a = b + c; //statement 3
        a = b + c + 5; //statement 4 /*compiler Error thrown -
passing 'const X' as 'this' argument of 'const X& X::operator+(int)' discards qualifiers
 [-fpermissive]*/
        (c = a + a) = b + c; //statement 5
}

根据我的理解(基于:+和=都是右关联的),以上5条陈述被解读为-

//statement 1
a.operator=(a.operator+(5.operator+(c)));

->我相信这应该抛出错误,因为没有定义的构造函数支持

//statement 2
a.operator=(b.operator+(5));

//statement 3 
a.operator=(a.operator=(b.operator+(c)));

//statement 4
a.operator=(b.operator+(c.operator+(5)));

//statement 5
(c.operator=(a.operator+(a)))=(b.operator+(c)); then
lhs_result.operator=(rhs_result);

我也正确地解读了上面第5条陈述吗?

如果要编译该语句4,则声明操作符,例如

const X& operator+ (int m) const;

问题是在此语句中

a = b + c + 5;

b + c是一个const对象。 请参阅相应的运算符声明

        const X& operator+ (const X& rhs) const;

您可能不会为const对象调用非const成员函数。

考虑到加法运算符+从左到右分组。 从C ++标准(5.7加法运算符)开始

1加法运算符+和-从左到右分组。

陈述5

(c = a + a) = b + c; //statement 5

则它具有未定义的行为,因为赋值运算符的操作数的评估未排序。 因此,将首先评估表达式(c = a + a),然后评估表达式b + c。 否则首先将评估表达式b + c,然后才评估表达式(c = a + a)

编译错误

a = b + c + 5;

您的operator +(int m)未标记为const 那就是编译器错误告诉您的。 您还应该仅返回X因为+计算新的X值。

X operator+ (int m) const;    

会解决的。

操作员关联

仅运算符=是右关联的。 http://en.cppreference.com/w/cpp/language/operator_precedence

评估顺序

  1. 您的陈述1

    a.operator =(a.operator +(5.operator +(C)));

我相信这应该抛出错误,因为没有定义的构造函数支持

这里没有构造函数。

  1. 声明4

    a.operator =(b.operator +(c.operator +(5)));
    应该

    a.operator =(b.operator +(C)+ .operator(5))

  2. 声明5

    (c = a + a)= b + c;
    由于赋值在括号内,因此优先于关联性。 c = a + a= b + c之前求值。

    (c.operator =(a.operator +(a)).operator =(b.operator +(c))

语句1这样做:

a.operator+(5).operator+(c)

这是可行的,因为采用Xoperator+const ,因此对a.operator+(5)返回的const临时a.operator+(5)

语句4这样做:

b.operator+(c).operator+(5)

这不起作用,因为采用intoperator+不是const ,因此不适用于b.operator+(c)返回的const临时b.operator+(c)

这两个operator+必须为const才能实现:

X operator+(const X& rhs) const;
X operator+(const int& m) const;

注意,我还将操作符更改为按值而不是引用返回。 那就是应该的样子(两个数字相加的结果毕竟是一个新数字)。

语句一等效于a.operator=(a.operator+(5).operator+(c)); 这是完全正确的。

但是,对于语句4, operator+ (int m)方法必须为const。 含义: const X& operator+ (int m) const;

语句5应该等效于: c.operator=(a.operator+(a)).operator=(b.operator+(c));

编译代码时clang的错误和警告非常有用,我建议将其用于此类实验。 它的错误和警告消息非常好:

test.cpp:15:13: error: invalid operands to binary expression ('const X' and 'int')
  a = b + c + 5; //statement 4 /*compiler Error thrown -
      ~~~~~ ^ ~
test.cpp:4:12: note: candidate function not viable: no known conversion from 'int' to 'const X' for 1st
      argument
  const X& operator+ (const X& rhs) const;
           ^
test.cpp:5:12: note: candidate function not viable: 'this' argument has type 'const X', but method is not
      marked const
  const X& operator+ (int m);
           ^
1 error generated.

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM