0

I'm looking at Linked Lists right now, and I'm wondering why this following function has a string being returned by reference? Why not just by value?

const string& front() const {
    return head->elem; 
}

What is the purpose of const string& vs const string?

eotsso
  • 1
  • 1
  • Don't make a copy if you don't need to. – Retired Ninja Sep 23 '22 at 04:43
  • Do you know why by reference is preferred over by value in general? [Is it more efficient to return a const reference](https://stackoverflow.com/questions/275795/is-it-more-efficient-to-return-a-const-reference) and [C++ Return value, reference, const reference](https://stackoverflow.com/questions/21778045/c-return-value-reference-const-reference) – Jason Sep 23 '22 at 04:44
  • Refer to [how to ask](https://stackoverflow.com/help/how-to-ask) where the first step is to *"search and then research"* and you'll find plenty of related SO posts for this. Also, refer to a [good c++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – Jason Sep 23 '22 at 04:48
  • I've seen passed by const reference all the time, and I do know why. I just never seen something being returned by const reference--so I was wondering. – eotsso Sep 23 '22 at 04:49

0 Answers0