c++ - When can I use a forward declaration? -


I am looking for definition when allowing me to announce a class forwarding in another category header file :

Do I allow it for a base class, a class organized as a member, to pass a member function from the context, etc.?

Keep yourself in the compiler's position: When you declare a type of forward, all the compilers Knows that this type is present; It does not know anything about its size, members, or methods, that is why it is called incomplete type , therefore, you do not use the type to declare a member, or base class Because the compiler will need to know the type of layout.

Assume the following forwarded declaration.

  square X;  

What can you do with an incomplete type:

Li>

incomplete type Announce a member to become an indicator or a reference to:

  class Foo {X * pt; X & PT; };  
  • ); X F2 ();

      zero f3 (X *, X & amp;) {} X & amp; F4 () {} X * F5 () {}  
  • What you can not do with an incomplete type:

    • Use it as a base class

        class Foo: X {} // compiler error!  
    • Use this to declare a member:

        square feet {X m; // Compiler Error! };  
    • define actions or methods that use this type

        zero f1 (xx) {} // compiler error! X F2 () {} // compiler error!             ; Zero method () {m-> Some method (); // Compiler Error! Int i = m- & gt; Some fields; // Compiler Error! }}; When it comes to a template, there is no absolute rule: whether you can use an incomplete type as a template parameter, depending on which  

      for example, std :: vector & lt; T & gt; Its parameters require a complete type, while boost :: container:: vector & lt; T & gt; Sometimes, if you use some member work then only one full type is required; for example.

      A well-documented template should point out all the requirements of their parameters in their documents, whether they are of full form or not.


    Comments

    Popular posts from this blog

    java - org.apache.http.ProtocolException: Target host is not specified -

    c# - Create dynamic LINQ query expression at runtime which translates into a parameterized SQL query for nullable types -

    cmake - cpack generates RPMs with %files entries that conflict with the RPM spec. How to fix? -