Understanding forward declarations of classes, pimpl

This is a discussion on Understanding forward declarations of classes, pimpl within the C++ Programming forums, part of the General Programming Boards category; I read that sometimes it's good to have two sets of header files: one for declarations and one for definitions, ...

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    54

    Understanding forward declarations of classes, pimpl

    I read that sometimes it's good to have two sets of header files: one for declarations and one for definitions, but I'm having trouble understanding exactly when and where a forward declaration of a class can be used instead of the actual definition.

    This is how I've always done things:
    Code:
    //myClass.h
    #ifndef _MYCLASS_H
    #define _MYCLASS_H
    
    class myClass { /* Class stuff here */ };
    
    #endif
    Code:
    //myClass.cxx
    #include "myClass.h"
    
    /* definitions of myClass member functions here */
    Code:
    // something like main.cxx or myOtherClass.h
    #include "myClass.h"
    
    /* Definitions of other classes and/or non-member functions that use or refer to myClass */

    Where and how would a header file consisting only of forward declarations come into play? I've always just #included the definition header file in every other file that used the class. From what I understand, this isn't ideal because every change to the class definition would require the recompilation of everything that included it.

    I'm trying to make something like myClassfwd.h with the forward declarations:
    Code:
    /* myClassfwd.h */
    #ifndef _MYCLASSFWD_H
    #define _MYCLASSFWD_H
    
    	/* Class forward declarations */
    	class myClass;
    
    #endif
    but I get compile-time errors like "error: invalid use of incomplete type" and "error: forward declaration of class blah blah blah" when I #include it instead of the definition header file. Including the myClass.h alongside the myClassfwd.h file gets rid of the errors, but obviously that's going against the whole purpose of it all.

    And I mentioned pimpl in the title of the post because I know it's somehow related to all this, but I'm so confused right now I think I'll settle for understanding the basics.
    Last edited by Boxknife; 04-22-2010 at 02:24 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,312
    Quote Originally Posted by Boxknife
    I'm having trouble understanding exactly when and where a forward declaration of a class can be used instead of the actual definition.
    Typical cases include: when you are declaring a pointer or reference to an object of that class; when you are declaring (but not defining) a function with a parameter of that class type.

    By the way, names that begin with an underscore followed by an uppercase letter, or that contain consecutive underscores, are reserved to the implementation for any use. Names that begin with an underscore are otherwise reserved to the implementation for use in the global and std namespaces. I suggest that you change your header inclusion guard naming convention.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jun 2008
    Posts
    54
    Thanks, it makes complete sense now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Does gcc hate Forward declarations?
    By SevenThunders in forum C++ Programming
    Replies: 12
    Last Post: 03-16-2009, 03:03 PM
  2. forward class declarations
    By manzoor in forum C++ Programming
    Replies: 17
    Last Post: 12-05-2008, 03:55 AM
  3. how do you resolve this error?
    By -EquinoX- in forum C Programming
    Replies: 32
    Last Post: 11-05-2008, 04:35 PM
  4. Forward declaration of classes
    By Elkvis in forum C++ Programming
    Replies: 21
    Last Post: 08-15-2008, 04:39 AM
  5. Forward Declarations in .net
    By Cornpops in forum C++ Programming
    Replies: 2
    Last Post: 04-01-2003, 02:22 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21