Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>EDIT: I experimented some, and discovered thing are a bit subtler than I thought. Here's what I now think is an accurate answer.</p> <p><code>&amp;s</code> is not an lvalue so you cannot create a reference to it unless the type of the reference is reference to <code>const</code>. So for example, you cannot do</p> <pre><code>string * &amp;r = &amp;s; </code></pre> <p>but you can do</p> <pre><code>string * const &amp;r = &amp;s; </code></pre> <p>If you put a similar declaration in the function header, it will work.</p> <pre><code>void myfunc(string * const &amp;a) { ... } </code></pre> <p>There is another issue, namely, temporaries. The rule is that you can get a reference to a temporary only if it is <code>const</code>. So in this case one might argue that &amp;s is a temporary, and so must be declared <code>const</code> in the function prototype. From a practical point of view it makes no difference in this case. (It's either an rvalue or a temporary. Either way, the same rule applies.) However, strictly speaking, I think it is not a temporary but an rvalue. I wonder if there is a way to distinguish between the two. (Perhaps it is simply defined that all temporaries are rvalues, and all non-lvalues are temporaries. I'm not an expert on the standard.)</p> <p>That being said, your problem is probably at a higher level. Why do you want a reference to the address of <code>s</code>? If you want a reference to a pointer to <code>s</code>, you need to define a pointer as in</p> <pre><code>string *p = &amp;s; myfunc(p); </code></pre> <p>If you want a reference to <code>s</code> or a pointer to <code>s</code>, do the straightforward thing.</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. 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