Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <pre><code>const int * const function_name(const int * point) const; //(1) (2) (3) (4) </code></pre> <p>The first prevents the returned pointer from being used to modify whatever data is being accessed by the function. This is good (unless you want to modify it), since it prevents unexpected modification of shared data.</p> <p>The second prevents the caller from modifying the returned pointer. This has no effect when returning a built-in type (like a pointer), and can be bad when returning a class type since it inhibits move semantics.</p> <p>The third prevents the function from modifying the data. This is good (when appropriate) for the same reason as the first.</p> <p>The fourth prevents the function from modifying the (non-mutable) state of the object. This is good (when appropriate) as it allows the function to be called on <code>const</code> objects, and makes it easier to keep track of when the object might change.</p> <pre><code>const int * const Functiontwo(const int * const &amp;) const; // (5) </code></pre> <p>The only new thing here is passing the pointer by <code>const</code> reference. There's no point in doing that; passing by value gives the same semantics, and is less verbose and probably more efficient. Passing by <code>const</code> reference <em>can</em> be more efficient than passing by value for types that are large or otherwise expensive to copy.</p> <p>A slight variation is to pass a <code>const</code> value:</p> <pre><code>const int * const Functiontwo(const int * const) const; // (6) </code></pre> <p>This is of no interest to the caller (indeed, the function can be declared with or without it without changing the meaning); so there's no point putting it on a <em>declaration</em>, just on the <em>definition</em>. It means that the pointer can't change within the function. This can be good (as can declaring local variables <code>const</code>), as it can make the function implementation easier to follow.</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