0

What I've understood, the reason is that we unnecessarily call the copy constructor for a simple statement like a=b; (both are objects).

What I don't get is that in my book it's written that we should never pass an object by reference, because as soon as the function terminates, that reference ceases to exist.

So is the text written in my book wrong or am I missing something here? Text ref: Overloading assignment operator in C++

  • 3
    you are quoting the book incomplete or the book is wrong. Returning objects by reference is fine. You shall not return the reference to an object that gets detroyed once the function returns (ie objects local to the function) – 463035818_is_not_an_ai Dec 04 '18 at 11:09
  • "Reference ceases to exist" is only for stack variables created inside that function. Here `*this` is not getting destroyed after the function returns. – Wander3r Dec 04 '18 at 11:11
  • if the text behind the link is relevant for your question you should include it in the question. Sorry, but one click away is already to far away – 463035818_is_not_an_ai Dec 04 '18 at 11:11
  • @user463035818 I've posted the related text completely. Thanks for the answer. – Shashank Kadambri Dec 04 '18 at 11:11
  • The text in your book is severely wrong. Get a new book. – Jean-Baptiste Yunès Dec 04 '18 at 11:11
  • The book's reasoning is sound. It explicitly says *"when a referenced object is created in the function, that reference ceases to exist when the function terminates"*. It's not quite technically correct, because the reference lives while the object is dead, but close enough for the purpose. The error is not in what the book said, but in applying it to a situation where the preconditions (local variable) don't hold. – nwp Dec 04 '18 at 11:12
  • @nwp The text is true if I return something local to the function, right? – Shashank Kadambri Dec 04 '18 at 11:15
  • @ShashankKadambri: Absolutely; don't return a reference to a variable with automatic storage duration. – Bathsheba Dec 04 '18 at 11:16
  • If you can afford it, pick a better book from [this list](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). Even a used earlier edition of any of those will be better than what you've got. – molbdnilo Dec 04 '18 at 11:28
  • @molbdnilo Thank you for the recommendation. Actually the book that I'm using is the course book which we are supposed to follow (for the exams ;)) – Shashank Kadambri Dec 04 '18 at 11:33

2 Answers2

4

There's nothing wrong with returning a reference from a function.

Indeed that's how the assignment operator operator= is normally defined (with return *this; for method chaining)!

What you shouldn't do is return a reference to an object that goes out of scope, e.g.

int& undefinedBehaviourServer()
{
    int ub;
    return ub;
}

In this case, ub has automatic storage duration and the returned reference to it will dangle.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
  • 2
    `return ub;` I see what you did there – bolov Dec 04 '18 at 11:18
  • Sorry but I don't know what automatic storage duration is. Can it be said that we should never return something local to the function by reference? – Shashank Kadambri Dec 04 '18 at 11:25
  • 1
    @ShashankKadambri: Have a Google: "automatic storage duration" is the formal term for a "local variable". It's what `auto` declared when I was a wee nipper. (From C++11 onwards `auto` means something entirely different). – Bathsheba Dec 04 '18 at 11:26
  • @Bathsheba Thank you very much for the clarification. I searched the term but I could not understand it. I would keep it your way. – Shashank Kadambri Dec 04 '18 at 11:29
-1

As soon as the function is completed, all objects declared in it are destroyed. Therefore, by returning a link from a function, you risk getting a call to a remote object. Let's see the typical example:

// don't do that!!!
std::string& get_str()
{
    std::string s = "abc";
    return s;
}


int main()
{
    string &s = get_str();
    // "abc"-string already destoyed at this moment
    std::cout << s; // attempt to deleted string: undefined behavior
}

Therefore, it is dangerous to return references to local objects from the functions, because it may involve accessing a deleted object (undefined behavior). Although technically returning an object (not local) reference is possible and often used. For example:

std::string& get_s()
{
    static std::string s = "abc";
    return s;
}
int main()
{
    std::string &s = get_s();
    std::cout << s; // that's OK
}
snake_style
  • 1,139
  • 7
  • 16
  • hm ok, one more nitpick: there is no "danger" in returning a reference to a (non-static) local. It is not something that might fail or that potentially is wrong. It is simply wrong. Always. – 463035818_is_not_an_ai Dec 04 '18 at 11:19
  • @user463035818 Thanks a lot, Luminary Magister! But "dangerous" not means wrong ;) – snake_style Dec 04 '18 at 11:23