Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Fundamentally the calling side has no distinct difference. But the <strong>decl</strong> side definitely does. As you have pointed out, references must be initialized to reference <em>something</em>. This makes them "safer", but even then there is no guarantee of "safety".</p> <p>The function pointer need NOT point to a function at all. It could be NULL, or even uninitialized (pointing to garbage). Not that it matters, because you can always change it later (something you can NOT do with references).</p> <pre><code>void (*s)(); // uninitialized </code></pre> <p>or</p> <pre><code>void (*s)() = NULL; // set to null </code></pre> <p>and later</p> <pre><code>void foo() { } s = foo; </code></pre> <p>You can do none of those with a reference. The reference <em>must</em> be initialized to <em>something</em> and preferabley something <em>valid</em>:</p> <pre><code>void (&amp;f)() = foo; // ok. also references foo(). void (&amp;f)() = *s; // also ok, but wait, is s valid?? does it have to be?? </code></pre> <p>However, even here a function reference is not guaranteed to be safe, just <em>safer</em>. You can certainly do this:</p> <pre><code>void (*s)(); void (&amp;f)() = *s; </code></pre> <p>You may get a compiler warning out of this (I do, "s used before being initialized") but in the end <code>f</code> still is now a reference to a "function" that <strong>isn't</strong> a function at all; just the random stack garbage in the <code>s</code> pointer. Worse, since references cannot be reassigned. this thing will <em>always</em> point at garbage. </p>
 

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