Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I think that your question is actually two questions:</p> <ul> <li>What are the implications of returning a const.</li> <li>What are the implications of returning a reference.</li> </ul> <p>To give you a better answer, I will explain a little more about both concepts.</p> <p><strong>Regarding the const keyword</strong></p> <p>The const keyword means that the object cannot be modified <strong>through</strong> that variable, for instance:</p> <pre><code>MyObject *o1 = new MyObject; const MyObject *o2 = o1; o1-&gt;set(...); // Will work and will change the instance variables. o2-&gt;set(...); // Won't compile. </code></pre> <p>Now, the const keyword can be used in three <em>different</em> contexts:</p> <ul> <li>Assuring the caller of a method that you won't modify the object</li> </ul> <p>For example:</p> <pre><code>void func(const MyObject &amp;o); void func(const MyObject *o); </code></pre> <p>In both cases, any modification made to the object will remain outside the function scope, that's why using the keyword const I assure the caller that I won't be modifying it's instance variables.</p> <ul> <li>Assuring the compiler that a specific method do not mutate the object</li> </ul> <p>If you have a class and some methods that "gets" or "obtains" information from the instance variables without modifying them, then I should be able to use them even if the const keyword is used. For example:</p> <pre><code>class MyObject { ... public: void setValue(int); int getValue() const; // The const at the end is the key }; void funct(const MyObject &amp;o) { int val = o.getValue(); // Will compile. a.setValue(val); // Won't compile. } </code></pre> <ul> <li>Finally, (your case) returning a const value</li> </ul> <p>This means that the returned object cannot be modified or mutated directly. For example:</p> <pre><code>const MyObject func(); void func2() { int val = func()-&gt;getValue(); // Will compile. func()-&gt;setValue(val); // Won't compile. MyObject o1 = func(); // Won't compile. MyObject o2 = const_cast&lt;MyObject&gt;(func()); // Will compile. } </code></pre> <p>More information about the const keyword: <a href="http://www.parashift.com/c++-faq-lite/const-correctness.html" rel="nofollow noreferrer">C++ Faq Lite - Const Correctness</a></p> <p><strong>Regarding references</strong></p> <p>Returning or receiving a reference means that the object will not be duplicated. This means that any change made to the value itself will be reflected outside the function scope. For example:</p> <pre><code>void swap(int &amp;x, int &amp;y) { int z = x; x = y; y = z; } int a = 2; b = 3; swap(a, b); // a IS THE SAME AS x inside the swap function </code></pre> <p>So, returning a reference value means that the value can be changed, for instance:</p> <pre><code>class Foo { public: ... int &amp;val() { return m_val; } private: int m_val; }; Foo f; f.val() = 4; // Will change m_val. </code></pre> <p>More information about references: <a href="http://www.parashift.com/c++-faq-lite/value-vs-ref-semantics.html" rel="nofollow noreferrer">C++ Faq Lite - Reference and value semantics</a></p> <p><strong>Now, answering your questions</strong></p> <pre><code>const int exampleOne(); </code></pre> <p>Means the object returned cannot change through the variable. It's more useful when returning objects.</p> <pre><code>int&amp; exampleTwo(); </code></pre> <p>Means the object returned is the same as the one inside the function and any change made to that object will be <em>reflected inside the function</em>.</p> <pre><code>const int&amp; exampleThree(); </code></pre> <p>Means the object returned is the same as the one inside the function and cannot be modified through that variable.</p>
    singulars
    1. This table or related slice is empty.
    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. This table or related slice is empty.
    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