Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here's the lowdown on all your cases:</p> <p><strong>• Return by reference</strong>: The function call can be used as the left hand side of an assignment. e.g. using operator overloading, if you have operator[] overloaded, you can say something like </p> <pre><code>a[i] = 7; </code></pre> <p>(when returning by reference you need to ensure that the object you return is available after the return: you should not return a reference to a local or a temporary)</p> <p><strong>• Return as constant value</strong>: Prevents the function from being used on the left side of an assignment expression. Consider the overloaded operator+. One could write something like:</p> <pre><code>a + b = c; // This isn't right </code></pre> <p>Having the return type of operator+ as "const SomeType" allows the return by value and at the same time prevents the expression from being used on the left side of an assignment.</p> <p>Return as constant value also allows one to prevent typos like these:</p> <pre><code>if (someFunction() = 2) </code></pre> <p>when you meant</p> <pre><code>if (someFunction() == 2) </code></pre> <p>If someFunction() is declared as </p> <pre><code>const int someFunction() </code></pre> <p>then the if() typo above would be caught by the compiler.</p> <p><strong>• Return as constant reference</strong>: This function call cannot appear on the left hand side of an assignment, and you want to avoid making a copy (returning by value). E.g. let's say we have a class Student and we'd like to provide an accessor id() to get the ID of the student:</p> <pre><code>class Student { std::string id_; public: const std::string&amp; id() const; }; const std::string&amp; Student::id() { return id_; } </code></pre> <p>Consider the id() accessor. This should be declared const to guarantee that the id() member function will not modify the state of the object. Now, consider the return type. If the return type were string&amp; then one could write something like:</p> <pre><code>Student s; s.id() = "newId"; </code></pre> <p>which isn't what we want.</p> <p>We could have returned by value, but in this case returning by reference is more efficient. Making the return type a const string&amp; additionally prevents the id from being modified.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload