Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Put yourself in the compiler's position: when you forward declare a type, all the compiler knows is that this type exists; it knows nothing about its size, members, or methods. This is why it's called an <em>incomplete type</em>. Therefore, you cannot use the type to declare a member, or a base class, since the compiler would need to know the layout of the type.</p> <p>Assuming the following forward declaration.</p> <pre><code>class X; </code></pre> <p>Here's what you can and cannot do.</p> <p><strong>What you can do with an incomplete type:</strong></p> <ul> <li><p>Declare a member to be a pointer or a reference to the incomplete type:</p> <pre><code>class Foo { X *pt; X &amp;pt; }; </code></pre></li> <li><p><em>Declare</em> functions or methods which accept/return incomplete types:</p> <pre><code>void f1(X); X f2(); </code></pre></li> <li><p><em>Define</em> functions or methods which accept/return pointers/references to the incomplete type (but without using its members):</p> <pre><code>void f3(X*, X&amp;) {} X&amp; f4() {} X* f5() {} </code></pre></li> </ul> <p><strong>What you cannot do with an incomplete type:</strong></p> <ul> <li><p>Use it as a base class</p> <pre><code>class Foo : X {} // compiler error! </code></pre></li> <li><p>Use it to declare a member:</p> <pre><code>class Foo { X m; // compiler error! }; </code></pre></li> <li><p><em>Define</em> functions or methods using this type</p> <pre><code>void f1(X x) {} // compiler error! X f2() {} // compiler error! </code></pre></li> <li><p>Use its methods or fields, in fact trying to dereference a variable with incomplete type</p> <pre><code>class Foo { X *m; void method() { m-&gt;someMethod(); // compiler error! int i = m-&gt;someField; // compiler error! } }; </code></pre></li> </ul> <hr> <p>When it comes to templates, there is no absolute rule: whether you can use an incomplete type as a template parameter is dependent on the way the type is used in the template.</p> <p>For instance, <code>std::vector&lt;T&gt;</code> requires its parameter to be a complete type, while <code>boost::container::vector&lt;T&gt;</code> does not. Sometimes, a complete type is required only if you use certain member functions; <a href="https://stackoverflow.com/questions/6012157/is-stdunique-ptrt-required-to-know-the-full-definition-of-t">this is the case for <code>std::unique_ptr&lt;T&gt;</code></a>, for example.</p> <p>A well-documented template should indicate in its documentation all the requirements of its parameters, including whether they need to be complete types or not.</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