Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In "place1", you need a reference. <code>Object1</code> isn't fully defined, so you can't take it by value. That said, you wouldn't want to pass by value; when you take the address of it, you'd be getting the address of the copy, not the actual object.</p> <p>Since you only want a pointer to a <code>BaseClass</code>, it might make more sense to only place that restriction on the parameter. (Of course, if you really need an <code>Object1</code>, reflect that in the type of <code>link</code>: <code>Object1* link;</code>).</p> <p>Also consider using an initialization list:</p> <pre><code>Object1(int a) : i(a), link(0) /* &lt;- important! */ { } Object1(int a, BaseClass&amp; o) : i(a), link(&amp;o) { } </code></pre> <p>Always use an initialize list.</p> <p>"place2" creates an unnamed instance of <code>Object1</code> using a constructor. It has <strike>no storage(?)</strike> <code>auto</code> storage, and <a href="https://stackoverflow.com/questions/2298781/why-do-un-named-c-objects-destruct-before-the-scope-block-ends/2298796"><em>dies at the end of the expression</em></a>.</p> <p>That is, once you've hit the semicolon in <code>Object1 a(1, /*place2*/ Object1(2));</code>, it ceases to exist and you have a pointer to an non-existent object! This results in undefined behavior.</p> <p>Give it a lifetime beyond the expression:</p> <pre><code>Object1 x(2); Object1 a(1, x); </code></pre> <p>Always make sure that when you refer an object, it will live the entire time you refer to it.</p> <hr> <p>Your new compile error is because you can't have a reference to a temporary variable. You <em>can</em> have one with a const-reference, though. The thing is, your pointer needs to point to a <code>const BaseClass</code> now, which may dampen what you want.</p> <p>Again, your design needs reworking, unfortunately. </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