Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ssl/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ c++;运算符+;=重载返回对的引用_C++_Operator Overloading - Fatal编程技术网

C++ c++;运算符+;=重载返回对的引用

C++ c++;运算符+;=重载返回对的引用,c++,operator-overloading,C++,Operator Overloading,有人能解释一下,在这个操作符重载的例子中,Average&operator+=(int-num)返回对Average的引用与不返回对Average的引用有什么不同,即Average operator+=(int-num) 返回引用是否意味着返回对分配给的对象(不是副本)的引用,该对象是*this。因此,在本例中,返回对对象avg的引用 非参考版本如何/为什么工作?将结果复制到哪里 #include <iostream> #include <cstdint> // for

有人能解释一下,在这个操作符重载的例子中,
Average&operator+=(int-num)
返回对
Average
的引用与不返回对
Average
的引用有什么不同,即
Average operator+=(int-num)

返回引用是否意味着返回对分配给的对象(不是副本)的引用,该对象是
*this
。因此,在本例中,返回对对象
avg
的引用

非参考版本如何/为什么工作?将结果复制到哪里

#include <iostream>
#include <cstdint> // for fixed width integers

class Average
{
private:
    int32_t m_total = 0; // the sum of all numbers we've seen so far
    int8_t m_numbers = 0; // the count of numbers we've seen so far

public:
    Average()
    {
    }

    friend std::ostream& operator<<(std::ostream &out, const Average &average)
    {
        // Our average is the sum of the numbers we've seen divided by the count of the numbers we've seen
        // We need to remember to do a floating point division here, not an integer division
        out << static_cast<double>(average.m_total) / average.m_numbers;

        return out;
    }

    // Because operator+= modifies its left operand, we'll write it as a member
    Average& operator+=(int num)
    {
        // Increment our total by the new number
        m_total += num;
        // And increase the count by 1
        ++m_numbers;

        // return *this in case someone wants to chain +='s together
        return *this;
    }
};

int main()
{
    Average avg;

    avg += 4;
    std::cout << avg << '\n';


    return 0;
}
#包括
#包含//用于固定宽度整数
班级平均
{
私人:
int32_t m_total=0;//到目前为止我们看到的所有数字的总和
int8\t m\u numbers=0;//到目前为止我们看到的数字计数
公众:
平均值()
{
}
friend std::ostream和操作员
返回一个引用是否意味着返回一个对被分配到的对象的引用(不是副本),这是*this。因此在这种情况下,返回一个对对象avg的引用

非参考版本如何/为什么工作?复制结果的位置

#include <iostream>
#include <cstdint> // for fixed width integers

class Average
{
private:
    int32_t m_total = 0; // the sum of all numbers we've seen so far
    int8_t m_numbers = 0; // the count of numbers we've seen so far

public:
    Average()
    {
    }

    friend std::ostream& operator<<(std::ostream &out, const Average &average)
    {
        // Our average is the sum of the numbers we've seen divided by the count of the numbers we've seen
        // We need to remember to do a floating point division here, not an integer division
        out << static_cast<double>(average.m_total) / average.m_numbers;

        return out;
    }

    // Because operator+= modifies its left operand, we'll write it as a member
    Average& operator+=(int num)
    {
        // Increment our total by the new number
        m_total += num;
        // And increase the count by 1
        ++m_numbers;

        // return *this in case someone wants to chain +='s together
        return *this;
    }
};

int main()
{
    Average avg;

    avg += 4;
    std::cout << avg << '\n';


    return 0;
}

返回的值是传递给
std::cout的临时对象。您应该返回一个引用,因为这是标准库中大多数代码使用的约定,大多数程序员都会使用。大多数程序员都希望得到以下代码:

std::string s;
(s += "a") = "b";
std::cout << s << std::endl;
预计将打印
100

这就是为什么您应该返回一个引用以保持约定,该约定允许修改赋值左侧的对象


否则,如果按值返回(副本)赋值在上面的例子中会被赋值给一个临时的.< /p> ,当C++中的操作符重写时,可以用多种形式提供它们。可以定义赋值操作符,这些赋值操作符不返回对修改对象的引用,它们将按预期工作。但是,我们对每个操作符都有我们所称的:

除了上面的限制之外,该语言对重载运算符的操作或返回类型没有其他限制(它不参与重载解析),但一般来说,重载运算符的行为应尽可能与内置运算符相似:
运算符+
应添加而不是乘以其参数,
运算符=
应赋值等。相关运算符的行为应类似(
operator+
operator+=
执行相同的加法操作)。返回类型受预期使用运算符的表达式的限制:例如,赋值运算符通过引用返回,以使写入
a=b=c=d
成为可能,因为内置运算符允许这样做

通常重载运算符具有以下典型的规范形式

关于这些规范形式有很多要读的,但我建议从这个非常好的答案开始


非参考版本如何/为什么工作

因为即使C++标准鼓励你使用规范形式,它也不会禁止你。 将结果复制到哪里

#include <iostream>
#include <cstdint> // for fixed width integers

class Average
{
private:
    int32_t m_total = 0; // the sum of all numbers we've seen so far
    int8_t m_numbers = 0; // the count of numbers we've seen so far

public:
    Average()
    {
    }

    friend std::ostream& operator<<(std::ostream &out, const Average &average)
    {
        // Our average is the sum of the numbers we've seen divided by the count of the numbers we've seen
        // We need to remember to do a floating point division here, not an integer division
        out << static_cast<double>(average.m_total) / average.m_numbers;

        return out;
    }

    // Because operator+= modifies its left operand, we'll write it as a member
    Average& operator+=(int num)
    {
        // Increment our total by the new number
        m_total += num;
        // And increase the count by 1
        ++m_numbers;

        // return *this in case someone wants to chain +='s together
        return *this;
    }
};

int main()
{
    Average avg;

    avg += 4;
    std::cout << avg << '\n';


    return 0;
}
在任何地方,该值都不会被丢弃。实现可能会将它们优化掉。

请尝试检查这一点

当您返回引用时,实际上是在传递实际对象。而当您按值返回时,将创建临时对象,然后将其传递给调用方

因此,如果您未经引用而返回,则此分配可能会按照上面的代码段进行

Average aa = avg += 4;
但如果您尝试,它将无法编译

Average *ptr = &(avg += 4);

如果您返回引用,上面的代码将起作用,因为我们正在传递对象的有效范围。

如果您的对象不可复制,会发生什么;但是
+=
有意义吗?