declaration and definition

This is a discussion on declaration and definition within the C++ Programming forums, part of the General Programming Boards category; Is there a performance penalty if I do function prototyping for non-member functions? Code: ( code above, classes, etc ) ...

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    46

    declaration and definition

    Is there a performance penalty if I do function prototyping for non-member functions?

    Code:
    ( code above, classes, etc )
    
    // Some comments
    void 
    Foo(int &v); // declare
    
    ( more code )
    
    void
    Foo(int &v)
    {
    // define 
    }
    Code:
    ( code above, classes, etc )
    
    // comments
    void
    Foo(int &v) // declares and defines
    {
    // do something
    }
    I believe the first code is better organized, especially if you have a lot of functions.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,830
    > Is there a performance penalty if I do function prototyping for non-member functions?
    No.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    242
    I believe the first code is better organized, especially if you have a lot of functions.
    me, too.
    in most cases, you actually have to do the first: if they're all in one file, then ( more code ) is either main or else class definitions relying on knowledge of Foo.
    but the file could also be just a list of functions relating to some issue, and Foo might be one that others don't depend on.
    i still prefer version 1, though. it seems to me (i'm no expert, so i'm serious about the "seems" there) that version 2 is making an inline declaration. and i think that in turn means that the funcion is compiled separately for each call. so, if you call it more than once, version 2 would also on that view make your compiled code longer, although i think maybe also nanoseconds faster at runtime.
    i'm not coming up with any circumstances where i like version 2 better, but that's not to say that they don't exist.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 02:29 PM
  2. Quantum Random Bit Generator
    By shawnt in forum C++ Programming
    Replies: 62
    Last Post: 06-18-2008, 11:17 AM
  3. failure to import external C libraries in C++ project
    By nocturna_gr in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2007, 03:49 PM
  4. Errors with including winsock 2 lib
    By gamingdl'er in forum C++ Programming
    Replies: 3
    Last Post: 12-05-2005, 08:13 PM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM

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