Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><code>const</code> after a member function means that the function does not modify the state of the parent object:</p> <pre><code>struct foo { int bar() const; // does not modify the instance of foo int &amp; bar(); // might modify the instance of foo }; </code></pre> <p>In your example your functions return </p> <pre><code>int const * const </code></pre> <p>The first <code>const</code> means, that the <code>int</code> you are pointing to must not be changed; you tell your user that he may read the value, but not modify it. This is also relevant for <code>const</code>-correctness:</p> <pre><code>int const * foo() const; // can be used from const functions int * foo(); // cannot be used from const function </code></pre> <p>The second <code>const</code> means that the pointer itself is <code>const</code>, meaning that:</p> <pre><code>int * ptr = &amp;a; *ptr = 42; // valid ptr = &amp;b; // valid int const * ptr = &amp;a; *ptr = 42; // INVALID ptr = &amp;b; // valid int * ptr const = &amp;a; *ptr = 42; // valid ptr = &amp;b; // INVALID int const * ptr const = &amp;a; *ptr = 42; // INVALID ptr = &amp;b; // INVALID </code></pre> <p>Making the pointer itself <code>const</code> as return value from a function is hardly ever useful.</p> <p>For references, a quick guide for C++03 is that you want to pass by reference if you want to modify the object, and want the modification to stick; you pass by value if you want to modify the object but do not want the modification to stick (a copy is made), or (exception!) if the value you are passing is a POD type; you pass by constant reference if you do not need to modify a non-POD type and thus save yourself the copy costs:</p> <pre><code>void foo(int &amp; a); // changes to a are applied to a itself, no copy made void foo(int a); // changes to a are applied to a copy of a, copy made void foo(std::string const &amp; a); // no changes allowed, no copy made </code></pre>
    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. VO
      singulars
      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