C++重载运算符执行顺序

关于c++重载运算符的问题

#include <iostream>
#include <cmath>
using namespace std;

const int n = 4;

struct binary_number
{
    bool bit[n];
};

istream &operator>>(istream &in, binary_number&);
ostream &operator<<(ostream &out, binary_number&);



istream &operator>>(istream &in, binary_number &a)
{
    
    char ch;
    for (int i = 0; i < n; i++)
    {
        in >> ch;
        if (ch == '0')
        {
            a.bit[i] = false;
        }
        else
        {
            a.bit[i] = true;
        }
    }
    return in;
}

ostream &operator<<(ostream &out, binary_number &a)
{
    for (int i = 0; i < n; i++)
    {
        out << a.bit[i];
    }
    return out;
}

binary_number full_adder(binary_number &b, binary_number &c)
{
    binary_number sum;
    binary_number cayin;
    for (int i = n - 1; i >= 0; i--)
    {
        sum.bit[i] = b.bit[i] ^ c.bit[i] ^ cayin.bit[i + 1];
        cayin.bit[i] = (b.bit[i] & c.bit[i]) | ((b.bit[i] ^ c.bit[i]) & cayin.bit[i + 1]);
    }
    return sum;
}

binary_number operator+(binary_number &a, binary_number &b)
{
    binary_number c;
    c = full_adder(a, b);
    return c;
}
void init(binary_number &ref)
{
    for (int i = 0; i < n; i++)
    {
        ref.bit[i] = false;
    }
}

int main ()
{
    binary_number a, b, c;
    init(a);
    init(b);
    init(c);
    c = a + b;
    cout << "a = " << a << endl;
    cout << "b = " << b << endl;
    cout << "c = " << a + b << endl;//报错行
    return 0;
}

请问各位在main函数中return 0;上面一行输出语句为什么不可以直接输出a+b,而要在前面先进行c=a+b的运算后再输出c,是因为运算符的先后顺序的问题吗?非常感谢解答困惑

修改如下:

#include <iostream>
#include <cmath>
using namespace std;
 
const int n = 4;
 
struct binary_number
{
    bool bit[n];
};
 
istream &operator>>(istream &in, binary_number&);
ostream &operator<<(ostream &out, const binary_number&); // 入参加个 const修饰
 
 
 
istream &operator>>(istream &in, binary_number &a)
{
    
    char ch;
    for (int i = 0; i < n; i++)
    {
        in >> ch;
        if (ch == '0')
        {
            a.bit[i] = false;
        }
        else
        {
            a.bit[i] = true;
        }
    }
    return in;
}
 
ostream &operator<<(ostream &out, const binary_number &a) // 入参加个 const修饰
{
    for (int i = 0; i < n; i++)
    {
        out << a.bit[i];
    }
    return out;
}
 
binary_number full_adder(binary_number &b, binary_number &c)
{
    binary_number sum;
    binary_number cayin;
    for (int i = n - 1; i >= 0; i--)
    {
        sum.bit[i] = b.bit[i] ^ c.bit[i] ^ cayin.bit[i + 1];
        cayin.bit[i] = (b.bit[i] & c.bit[i]) | ((b.bit[i] ^ c.bit[i]) & cayin.bit[i + 1]);
    }
    return sum;
}
 
binary_number operator+(binary_number &a, binary_number &b)
{
    binary_number c;
    c = full_adder(a, b);
    return c;
}
void init(binary_number &ref)
{
    for (int i = 0; i < n; i++)
    {
        ref.bit[i] = false;
    }
}
 
int main ()
{
    binary_number a, b, c;
    init(a);
    init(b);
    init(c);
    c = a + b;
    cout << "a = " << a << endl;
    cout << "b = " << b << endl;
    cout << "c = " << (a + b) << endl;
    return 0;
}
 

  • 以下回答由chatgpt基于相关博客总结生成:

    C++重载运算符的执行顺序按照优先级从高到低为: () [] -> . ++ -- ~ !(一元) &(取地址、解引用)new delete + - * / % + - << >> < <= > >= == != & ^ | && || ?: = += -= = /= %= <<= >>= &= ^= |= ,。因此,为了确保加法运算的执行顺序,需要使用括号来明确优先级,即先计算a+b,然后赋值给c,最后输出c。代码示例:

    #include <iostream>
    using namespace std;
    
    class Number {
    public:
        int val;
        Number() {}
        Number(int v): val(v) {}
        Number operator +(const Number& n) const {
            return Number(this->val + n.val);
        }
    };
    
    int main() {
        Number a(1), b(2);
        Number c = a + b;
        cout << c.val << endl;
        return 0;
    }