View Poll Results: Should people include prototypes in headers?

Voters
15. You may not vote on this poll
  • Yeah

    12 80.00%
  • No

    3 20.00%
  • No but I do it anyway

    0 0%

What is your view on having the protoype in headers

This is a discussion on What is your view on having the protoype in headers within the C++ Programming forums, part of the General Programming Boards category; So some people have been telling me not to include code in my header files, I do it because it ...

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    241

    What is your view on having the protoype in headers

    So some people have been telling me not to include code in my header files, I do it because it is more convinent. But I have been thinking and might go back to using the source for all the prototypes and coding but I just want to know, am I the only who does/has done this?

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Are you sure you mean "Should people include prototypes in headers?" That's what headers are for. You put the prototype in the header and the implementation of the function in the source file.

    The question should be "Should people include function definitions in headers?" And the answer is no in most cases.

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    241
    Yeah, I have to change that :-p

    Urg I can't... can any mod change it for me and reset the votes?

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    There are no votes to reset.

    BTW, your way of doing things won't work if you have multiple source files that include that header. The only reason you would put the implementations into the header as you have is the actual reason you are doing it- to make the source file smaller. Of course, it still goes against common practices.

    The normal way to break up a source file like that is to create another source file with the function definitions, and the header with the prototypes, then compile both source files and link them together.

  5. #5
    Registered User
    Join Date
    Sep 2005
    Posts
    241
    Quote Originally Posted by Daved
    There are no votes to reset.

    BTW, your way of doing things won't work if you have multiple source files that include that header. The only reason you would put the implementations into the header as you have is the actual reason you are doing it- to make the source file smaller. Of course, it still goes against common practices.

    The normal way to break up a source file like that is to create another source file with the function definitions, and the header with the prototypes, then compile both source files and link them together.
    Is that like making a DLL or something? Or does it all go in to one exe... also what are the benifits of using dlls? (I've been pondering them for a while)

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    That has nothing to do with DLLs but, yes, the concept is similar in some ways. You are modularizing your code so that changes to the implementation of one part don't affect another.

  7. #7
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,475
    It can also depend on the size of the application. For say, a commercial game, then I would say most companies do not include prototypes in headers, although it must be pointed out that one company would code different from another, it is all a matter of personal taste

  8. #8
    #define WORLD "sad place" LinuxCoder's Avatar
    Join Date
    Mar 2006
    Location
    Portugal
    Posts
    89
    Just for the sake of clarity, let's define what a function prototype is. I've learned that:
    Code:
    int myfn(int x);
    is a function prototype, it tells the compiler what a function will return and what arguments must be passed on a call to it. It is used in header files, class method declarations and for forward declarations. while:
    Code:
    int myfn(int x)
    {
        // do something in here
    }
    would be called the function declaration or implementation (well, it contains a function prototype).

    I might be wrong but this is the way i have learned, so a header file will most of the times (there will always be exceptions) include function prototypes, but only in some cases will it contain function implementations.

    Again, this is how i've learned but i'm no c++ master by any means.

    P.S - If what i've learned is right then Daved is correct and the question is in itself wrong.

    Cheers

  9. #9
    Registered User
    Join Date
    Sep 2005
    Posts
    241
    Quote Originally Posted by LinuxCoder
    Just for the sake of clarity, let's define what a function prototype is. I've learned that:
    Code:
    int myfn(int x);
    is a function prototype, it tells the compiler what a function will return and what arguments must be passed on a call to it. It is used in header files, class method declarations and for forward declarations. while:
    Code:
    int myfn(int x)
    {
        // do something in here
    }
    would be called the function declaration or implementation (well, it contains a function prototype).

    I might be wrong but this is the way i have learned, so a header file will most of the times (there will always be exceptions) include function prototypes, but only in some cases will it contain function implementations.

    Again, this is how i've learned but i'm no c++ master by any means.

    Cheers
    Yeah I know, I got mixed up...

  10. #10
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    only put inline code in headers, otherwise you will get duplicate definition errors. inline is normal in c++ headers and defined within c++ classes.

  11. #11
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,475
    Inline can speed up a program, but requires more typing

  12. #12
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> it is all a matter of personal taste
    It is not exactly up to personal taste in this instance. Assuming you meant definitions instead of prototypes, there are reasons why the definitions go into the source file (in most cases). If you put all your definitions in the header file, it defeats the purpose of the system. Technically it is up to personal taste whether you want to use the features available for their intended use, but it is still the "wrong" thing to do.

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,648
    I place my class/function template definitions in headers, though there is the technique of placing them in source files and then including the source file in the header.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    Templates are the only C++ mechanisms that don't follow the declarations in headers, definitions in source file rule.

    Other than that you can inline your code in C++ headers which is nice, but don't overuse it or you'll be flipping between CPP and HPP or H files to find your function bodies.

    This is not a matter of personal style or preference. If you are sticking large amounts of non-class member function bodies in header files you are using them incorrectly.

  15. #15
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    I have a personal limt size for inline functions of 1 statement. If its more than 1 statement then I make it a normal function or class method. If it is only 1 statement then I normally inline it.

    Microsoft ATL/COM wizard is really netorious for putting lots of code in header files. I normally move that code to the implementation *.cpp file where most programmers I know would normally put it.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Tree View Control flickering
    By cfriend in forum Windows Programming
    Replies: 1
    Last Post: 09-13-2004, 09:25 AM
  2. Tree View control not appearing
    By Garfield in forum Windows Programming
    Replies: 1
    Last Post: 03-07-2004, 12:47 PM
  3. Headers that use each other
    By nickname_changed in forum C++ Programming
    Replies: 7
    Last Post: 10-03-2003, 04:25 AM
  4. Set View Position in CEditView :: MFC
    By kuphryn in forum Windows Programming
    Replies: 1
    Last Post: 05-21-2002, 09:27 PM
  5. Determining Active View :: MFC
    By kuphryn in forum Windows Programming
    Replies: 0
    Last Post: 04-25-2002, 07:34 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21