Advice on structured header use
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10

Thread: Advice on structured header use

  1. #1
    Join Date
    May 2013
    Posts
    24

    Advice on structured header use

    Hello,

    I am a self taught newbie in C/C++ with some holes in my knowledge. I am looking for advice on how to structure my headers for a C/C++ project that I am working on in VS2008. I would like to port some of the code to another CPU architecture, probably ARM so portability is an issue. For this reason the portable code is written in C with the GUI diagnostics written in C++/MFC.

    Your advice about how you structure your headers would be appreciated.

  2. #2
    Join Date
    May 2013
    Posts
    24

    Re: Advice on structured header use

    Found this advice: http://www.umich.edu/~eecs381/handou...Guidelines.pdf
    What are your thoughts.

  3. #3
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,316

    Re: Advice on structured header use

    Looks fine, though what the author calls "structure type declarations" are actually structure definitions (which admittedly are also declarations).

    Also, in the case of C++, the use of an unnamed namespace is preferred to declaring helper functions static.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  4. #4
    Join Date
    May 2013
    Posts
    24

    Re: Advice on structured header use

    Thank you laserlight for your response. How many of the rules do you think are based upon the precept of hiding the data structures and code? I would be interested in your opinion.

  5. #5
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,014

    Re: Advice on structured header use

    Quote Originally Posted by moffy View Post
    Found this advice: http://www.umich.edu/~eecs381/handou...Guidelines.pdf
    What are your thoughts.
    Most of these are good guidelines. The second (header guards) could be replaced by using "#pragma once" if it is supported on all target compilers. This avoids the hassle of having to come up with unique identifiers for each header.
    The first guideline ("Each module with its .h and .c file should correspond to a clear piece of functionality.") can be interpreted quite differently depending on the programming paradigm being followed. In this sense, it is important to realize that there is no "C/C++" language. C and C++ are different languages with different rules and guidelines. In C++, when predominantly using an OOP style, this guideline can often be interpreted as "one struct/class definition per header file (not counting nested structs/classes)". In C obviously, global functions are needed and often a more functional/procedural programming style is followed, thus one would group sets of related functions in header files.
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  6. #6
    Join Date
    May 2013
    Posts
    24

    Re: Advice on structured header use

    Thank you D_Drmmr for your advice.

    I have been thinking about the differences that result from as you mentioned C++ which is OOP and C which is function based. In C++ it makes good sense to hide internal structures and procedures and only expose the public methods of the class because the "class" encapsulates not only the code but the data.

    In C it's a bit different as long as you are not writing a LIB or DLL, in which you only expose the public functions. But in a C project, one for internal use rather than public, you tend to have unrestricted access to all functions and data. For an embedded project this can greatly speed up the code as you are not restricted to a class I/O function. I am probably not expressing this as fully as I would like nor as accurately but I hope you can discern my meaning.

    Just a thought.

  7. #7
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,014

    Re: Advice on structured header use

    Quote Originally Posted by moffy View Post
    I have been thinking about the differences that result from as you mentioned C++ which is OOP and C which is function based. In C++ it makes good sense to hide internal structures and procedures and only expose the public methods of the class because the "class" encapsulates not only the code but the data.
    C++ is not just object oriented; it's multi-paradigm. Procedural and generic programming is also fully supported by C++. There are many C++ libraries that are header-only, notably the Boost libraries. When you make heavy use of templates, the guidelines for header use change. That's why I said the interpretation depends on the programming paradigm being used, rather than the programming language being used.
    Quote Originally Posted by moffy View Post
    In C it's a bit different as long as you are not writing a LIB or DLL, in which you only expose the public functions. But in a C project, one for internal use rather than public, you tend to have unrestricted access to all functions and data. For an embedded project this can greatly speed up the code as you are not restricted to a class I/O function. I am probably not expressing this as fully as I would like nor as accurately but I hope you can discern my meaning.
    In C, it is also possible to emulate the use of public/private functions by hiding a struct definition from the header file.
    Code:
    // header file
    struct Example;
    
    Example* CreateExample(int a); // emulate c'tor
    void ReleaseExample(Example* pThis); // emulate d'tor
    int ExampleFoo(Example* pThis, int b); // emulate member function
    
    // .c file
    struct Example
    {
        int a;
    };
    
    Example* CreateExample(int a)
    {
        Example* p = (Example*)malloc(sizeof(Example));
        p->a = a;
        return p;
    }
    // etc.
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  8. #8
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,023

    Re: Advice on structured header use

    Quote Originally Posted by moffy View Post
    In C it's a bit different as long as you are not writing a LIB or DLL, in which you only expose the public functions. But in a C project, one for internal use rather than public, you tend to have unrestricted access to all functions and data.
    I would dare to disagree. It highly depends on the way how you design data types and interfaces. Please consider the plain C sample (monolithic, not a LIB or DLL) attached.
    Attached Files Attached Files
    Best regards,
    Igor

  9. #9
    Join Date
    May 2013
    Posts
    24

    Re: Advice on structured header use

    Thank you for your advice.
    1.D_ Drmmr: I have been doing something similar in C to the constructor/destructor you illustrated. Very handy when it comes to cleaning up memory structures. If you change the structure you only need to modify 2 places. Only need to debug 2 pieces of code, very handy.

    2. Igor thank you for the example. I am aware that whatever you can do in C++ you can do in C, I guess the C++ compiler was originally written in C. It's more a matter of style rather than potential I was referring to. But point taken and thanks again.

  10. #10
    Join Date
    May 2013
    Posts
    24

    Re: Advice on structured header use

    Thank you for all your advice. I am in the middle of a rewrite and have found the rules listed in post#2 valuable. I pretty much have to agree with them all. I especially like the forward declaration e.g. "struct blog_t;" and then using pointers e.g. blog_t *, to access it. Nice.

    I have one further question. What do you do with your general macros like MAX(a,b), MIN(a,b). My inclination is to create a single macro.h file and include that.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center