Thread: including headers and forward declaration

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    630

    including headers and forward declaration

    Hello

    I'm always having problems with including headers in my projects and forward declarations.
    I always spend too much time before I get my problem solved and program compiled with 0 errors.

    The questions Im unsure about.. When am I allowed to do forward declaration in header? When I have pointer or reference? Is this right?

    Suppose I have:

    Code:
    class someclass {
       someobject obj;
    };
    In that case I have to include header where someobject is declared?

    Any good tips would be really welcome

    Thanks for help

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Your understanding seems right.

    You have to include the header whenever the compiler needs the definition of the class. If there is just a pointer or reference, it doesn't need to know anything about the class. If you have an object like in your example, it needs the definition of the class to know its size and possibly to be able to call its constructors and destructors in the outer class's default constructors and destructor.

    Another time the header needs to be included is if you have member functions defined inline in the class definition. So in this case you would need to include the header (although a better solution might be to move the function definition to the source file):
    Code:
    class someclass {
       someobject* pobj;
       void doit()
       {
          if (pobj)
             pobj->foo();
       }
    };
    In that case the compiler needs the definition of someobject to verify that foo() is an actual member function.

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    630
    I noticed I've been using forward declaration in my project in wrong way.
    I did forward declaration although I should include the header instead, but everything compiled just fine.. I wonder why..

    For instance:

    Code:
    class forward;
    
    class someclass {
    public:
        forward m_f;
    };
    Of course this is simplified code just for demonstration.

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    No, this snippet doesn't compile as it is. There must have been something else going on (for example another included header including the header where forward was prototyped).
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    Registered User
    Join Date
    May 2006
    Posts
    630
    Yes, I think it was. Thank you both for explanation.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM