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%

Thread: What is your view on having the protoype in headers

  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,366
    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,366
    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,366
    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,688
    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,688
    Inline can speed up a program, but requires more typing

  12. #12
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> 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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    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.

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, 01: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