Header file best practices for typedefs

前端 未结 5 1328
温柔的废话
温柔的废话 2020-12-13 01:51

I\'m using shared_ptr and STL extensively in a project, and this is leading to over-long, error-prone types like shared_ptr< vector< shared_ptr

5条回答
  •  无人及你
    2020-12-13 02:30

    I'm programming on a project which sounds like it uses the common.h method. It works very well for that project.

    There is a file called ForwardsDecl.h which is in the pre-compiled header and simply forward-declares all the important classes and necessary typedefs. In this case unique_ptr is used instead of shared_ptr, but the usage should be similar. It looks like this:

    // Forward declarations
    class ObjectA;
    class ObjectB;
    class ObjectC;
    
    // List typedefs
    typedef std::vector> ObjectAList;
    typedef std::vector> ObjectBList;
    typedef std::vector> ObjectCList;
    

    This code is accepted by Visual C++ 2010 even though the classes are only forward-declared (the full class definitions are not necessary so there's no need to include each class' header file). I don't know if that's standard and other compilers will require the full class definition, but it's useful that it doesn't: another class (ObjectD) can have an ObjectAList as a member, without needing to include ObjectA.h - this can really help reduce header file dependencies!

    Maintenance is not particularly an issue, because the forwards declarations only need to be written once, and any subsequent changes only need to happen in the full declaration in the class' header file (and this will trigger fewer source files to be recompiled due to reduced dependencies).

    Finally it appears this can be shared between projects (I haven't tried myself) because even if a project does not actually declare an ObjectA, it doesn't matter because it was only forwards declared and if you don't use it the compiler doesn't care. Therefore the file can contain the names of classes across all projects it's used in, and it doesn't matter if some are missing for a particular project. All that is required is the necessary full declaration header (e.g. ObjectA.h) is included in any source (.cpp) files that actually use them.

提交回复
热议问题