简体   繁体   English

返回类型不匹配(或不匹配)

[英]Return type mismatch (or not)

I'm a bit confused by this piece of code. 我对这段代码有些困惑。 It's actually mine, but still I couldn't understand why this compiles without at least any warning. 它实际上是我的,但我仍然不明白为什么在没有任何警告的情况下进行编译。

#include <iostream>
class Line {
    private:
        int length;
    public:
        Line(void);
        Line(int);
        int getLength(void);
        Line& operator = (const Line&);
};
Line::Line(int a) : length(a) {}
int Line::getLength(void) { return length; }
Line& Line::operator = (const Line& line)           // The function return type is reference of a value of the Line type.
{
    length = line.length;
    return *this;                                   // The function actually returns dereferenced (*this) value.
}
int main(void)
{
    Line line {2};
    Line line_a {0};
    line_a = line;
    std::cout << line_a.getLength() << std::endl;
    return 0;
}

The only difference is the absence of ampersand. 唯一的区别是没有“&”号。 It still compiles 它仍然可以编译

#include <iostream>
class Line {
    private:
        int length;
    public:
        Line(void);
        Line(int);
        int getLength(void);
        Line operator = (const Line&);
};
Line::Line(int a) : length(a) {}
int Line::getLength(void) { return length; }
Line Line::operator = (const Line& line)            // The function return type is reference of a value of the Line type.
{
    length = line.length;
    return *this;                                   // The function actually returns dereferenced (*this) value.
}
int main(void)
{
    Line line {2};
    Line line_a {0};
    line_a = line;
    std::cout << line_a.getLength() << std::endl;
    return 0;
}

this is a pointer to the current object. this是指向当前对象的指针。 Dereferencing this gives you that object. 取消引用this为您提供了对象。 When the return type of the function is a reference and you return an object, a reference to that object is returned instead. 当函数的返回类型是引用并且您返回对象时,将返回对该对象的引用。 So, you are returning a reference to the object that you got from dereferencing this pointer. 因此,您将返回对从取消引用this指针获得的对象的引用。 No reason for any warnings. 没有任何警告的理由。

If you didn't dereference this pointer, then you'd get a compilation error because you'd be trying to return (a reference to) a pointer which would conflict with the return type of the function. 如果不取消引用this指针,则将遇到编译错误,因为您将尝试返回(对它的引用)与该函数的返回类型相冲突的指针。

Let's consider this code: 让我们考虑以下代码:

#include <iostream>
int global_variable = 8;
int function(void)
{
    return global_variable;
}
int main(void)
{
    function() = 16;        // Compiling this code would give error: lvalue required as left operand of assignment.
    std::cout <<  global_variable << std::endl;
    return 0;
}

But, if we modify this code little bit and we would change the function return type to reference to int - this will change lvalue to rvalue ( int somenumber is lvalue , int& somenumber is rvalue ). 但是,如果我们稍稍修改此代码,然后将函数返回类型更改为对int的引用-这会将lvalue更改为rvalueint somenumberlvalueint& somenumberrvalue )。

#include <iostream>
int global_variable = 8;
int& function(void)
{
    return global_variable;
}
int main(void)
{
    function() = 16;
    std::cout <<  global_variable << std::endl;      // The global_variable becomes 16! (it was 8);
    return 0;
}

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

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