Declarations and Definitions in One Header

Discussion in 'C++' started by Steven T. Hatton, Apr 25, 2004.

  1. Now why didn't *_I_* think of that!

    Declarations and Definitions in One Header

    C++ object definitions can be quite complex. In principle, your source code
    will need two kinds of things for each object that you use across more than
    one source file. First, you need an interface specification, describing its
    structure with type declarations and function prototypes. Second, you need
    the implementation itself. It can be tedious to maintain a separate
    interface description in a header file, in parallel to the actual
    implementation. It is also dangerous, since separate interface and
    implementation definitions may not remain parallel.

    http://tinyurl.com/2xx3w

    --
    STH
    Hatton's Law: "There is only One inviolable Law"
    KDevelop: http://www.kdevelop.org SuSE: http://www.suse.com
    Mozilla: http://www.mozilla.org
     
    Steven T. Hatton, Apr 25, 2004
    #1
    1. Advertising

  2. Steven T. Hatton

    JKop Guest

    They're called "inline" functions.
     
    JKop, Apr 25, 2004
    #2
    1. Advertising

  3. Steven T. Hatton

    Karthik Guest

    Steven T. Hatton wrote:
    > Now why didn't *_I_* think of that!
    >
    > Declarations and Definitions in One Header
    >
    > C++ object definitions can be quite complex. In principle, your source code
    > will need two kinds of things for each object that you use across more than
    > one source file. First, you need an interface specification, describing its
    > structure with type declarations and function prototypes. Second, you need
    > the implementation itself. It can be tedious to maintain a separate
    > interface description in a header file, in parallel to the actual
    > implementation. It is also dangerous, since separate interface and
    > implementation definitions may not remain parallel.
    >
    > http://tinyurl.com/2xx3w
    >


    This is more of a question of style and preference, rather than a
    general rule . As a thumb rule, in C++ - the header files are always
    separated from source files (with proper header guards that is, in the
    header files).

    But then you were right when you mentioned about separating interface
    and the implementation. Interestingly, Java does not allow you to do
    that. When you define classes, you got to write the define the methods
    along with the declarations, there is way we can separate them. ( The
    'interface' keyword is entirely different though ).



    --
    Karthik

    ------

    Human Beings please 'removeme' for my email.
     
    Karthik, Apr 25, 2004
    #3
  4. Steven T. Hatton

    Rolf Magnus Guest

    Steven T. Hatton wrote:

    > Now why didn't *_I_* think of that!
    >
    > Declarations and Definitions in One Header
    >
    > C++ object definitions can be quite complex. In principle, your source
    > code will need two kinds of things for each object that you use across
    > more than one source file. First, you need an interface specification,
    > describing its structure with type declarations and function
    > prototypes. Second, you need the implementation itself. It can be
    > tedious to maintain a separate interface description in a header file,
    > in parallel to the actual implementation.


    Actually, I see an advantage in doing that. It's cleaner to separate the
    two, IMHO.

    > It is also dangerous, since separate interface and implementation
    > definitions may not remain parallel.


    Why would that be dangerous? If they don't match, the compiler will tell
    you so with an error message.
     
    Rolf Magnus, Apr 25, 2004
    #4
  5. Karthik wrote:

    > Steven T. Hatton wrote:
    >> Now why didn't *_I_* think of that!


    >> http://tinyurl.com/2xx3w


    >
    > This is more of a question of style and preference, rather than a
    > general rule . As a thumb rule, in C++ - the header files are always
    > separated from source files (with proper header guards that is, in the
    > header files).


    I hope you clicked the link. I should have put quotation marks in the
    paragraph. It's from the latest GNU gcc documentation. I didn't write it.

    > But then you were right when you mentioned about separating interface
    > and the implementation. Interestingly, Java does not allow you to do
    > that. When you define classes, you got to write the define the methods
    > along with the declarations, there is way we can separate them. ( The
    > 'interface' keyword is entirely different though ).


    You can separate interface from implementation in Java. AAMOF the official
    Java recommendation is this: "Every major class in an application should be
    an implementation of some interface that captures the contract of that
    class." TJPL(3E) page 105. There are also other strategies such as that
    used with JAXB. I find that approach to be far more trouble than it's
    worth. For the most part, I believe a properly designed class will provide
    its own interface without the need to resort to abstracting it.

    There is an advantage to implementing a standardized API in the abstract,
    and having various implementations created to back it up.

    --
    STH
    Hatton's Law: "There is only One inviolable Law"
    KDevelop: http://www.kdevelop.org SuSE: http://www.suse.com
    Mozilla: http://www.mozilla.org
     
    Steven T. Hatton, Apr 25, 2004
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rikard Land
    Replies:
    0
    Views:
    353
    Rikard Land
    Oct 19, 2004
  2. Chris Gordon-Smith

    Declarations and Definitions: Grammar

    Chris Gordon-Smith, Oct 3, 2004, in forum: C++
    Replies:
    2
    Views:
    385
    Gary Labowitz
    Oct 3, 2004
  3. aaragon
    Replies:
    14
    Views:
    414
  4. Jess
    Replies:
    15
    Views:
    517
  5. panku

    declarations and definitions

    panku, Feb 1, 2011, in forum: C Programming
    Replies:
    6
    Views:
    307
    David Thompson
    Feb 11, 2011
Loading...

Share This Page