Another approach is suggested by Herb Sutter in "Exceptional C++". See "the
pimpl idiom".

This is a summary:

It is good to minimize h file dependencies, and it is good to hide private
data from other classes. However, C++ doesn't have a good internal mechanism
to do this. The following style approximates it:

---- A.h ----

        class AImpl;    // forward declaration

        class A
        {
        public:
                ... // all public access functions
        private:
                AImpl* m_pImpl;
        };

---- A.cpp ----

        #include "B.h"

        class AImpl
        {
        public:
                B m_B;
        };

        A::A()
                : m_pImpl(new AImpl)
        {}

        A::~A()
        {
                delete m_pImpl;
        }

        void A::Function()
        {
                m_pImpl->m_B;   // use B somehow.
        }               

It seems like a bit of extra work, and an extra memory allocation, but after
using it some, I just love the streamlined H files. It makes developing
client code much clearer.

> -----Original Message-----
> From: [EMAIL PROTECTED] 
> [mailto:[EMAIL PROTECTED] On Behalf Of Ehsan Akhgari
> Sent: Tuesday, January 25, 2005 11:40 PM
> To: [email protected]
> Subject: RE: [msvc] header file inclusion
> 
> 
> > class A
> > {
> > .....
> > private:
> >     class B;
> > };
> >
> > Assume A is defined in A.h and B is defined in B.h Here the
> > composition of A depends on the type B.
> >
> > Say code in file "one.cpp" wants to create objects of type A.
> > 1) Either in one.cpp we first include B.h followed by A.h OR
> > 2) We include B.h in the file A.h.
> >
> > Which approach is better ? I feel per module there must be a common
> > header file. Also, is it absolutely evil/bad to include header files
> > within header files?
> 
> Actually, I would #include "B.h" in A.h, so that anywhere you 
> include A.h,
> it just works without you needing to worry about what headers 
> should be
> included before it.
> 
> You should just use #include guards, or #pragma once to make 
> sure the same
> header is not included multiple times in a translation unit.
> 
> -------------
> Ehsan Akhgari
> 
> Farda Technology (http://www.farda-tech.com/)
> 
> List Owner: [email protected]
> 
> [ Email: [EMAIL PROTECTED] ]
> [ WWW: http://www.beginthread.com/Ehsan ]
> 
> Under conditions of peace the warlike man attacks himself.
> -Beyond Good And Evil, F. W. Nietzsche
> 
> 
> 
> 
> _______________________________________________
> msvc mailing list
> [email protected]
> See 
> http://beginthread.com/mailman/listinfo/msvc_beginthread.com 
> for subscription changes, and list archive.
> 



_______________________________________________
msvc mailing list
[email protected]
See http://beginthread.com/mailman/listinfo/msvc_beginthread.com for 
subscription changes, and list archive.

Reply via email to