Forward Declaration

When you use a class A, defined in A.hpp, one may not need to include this file. This is the case when in our file we do not use any information about the class structure (except the name). This is the case when we define a class B which has class A pointers, or methods that take pointers to class A. Note that inside theses methods we cannot use methods of class A, because we would be using information from this class. Note also that we need always to work with pointers, otherwise, the compiler would need the size of the structure, which is itself an information about the class structure.

There’s a policy to avoid including headers whenever forward declaration is possible, since it can slow down compilation. I’m not sure whether this is a common situation, in such a way that requires special attention in code style rules. Nevertheless, here is a clear example of an application of forward declaration:

http://www-subatech.in2p3.fr/~photons/subatech/soft/carnac/CPP-INC-1.shtml

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s