Davycc
Davycc

Reputation: 73

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;
}

Upvotes: 0

Views: 459

Answers (2)

Davycc
Davycc

Reputation: 73

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).

#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;
}

Upvotes: 0

eerorika
eerorika

Reputation: 238311

this is a pointer to the current object. Dereferencing this gives you that object. 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. 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.

Upvotes: 1

Related Questions