Where to put local auxiliary functions?

This is a discussion on Where to put local auxiliary functions? within the C++ Programming forums, part of the General Programming Boards category; Hi everyone, I've taken a C++ class this semester and now I'm writing the seminal program, which is my first ...

  1. #1
    Registered User draugr's Avatar
    Join Date
    Mar 2009
    Posts
    9

    Question Where to put local auxiliary functions?

    Hi everyone,

    I've taken a C++ class this semester and now I'm writing the seminal program, which is my first large-ish C++ project.

    Usually it's a good idea to split code into smaller auxiliary functions when something grows too large. The question I want to ask is where is a good place to put those.

    Say I have a .cpp and .hpp files with some class, and I'd like to make a function to perform some task that will not be needed anywhere else but some method or whatever. The obvious thing to do would be simply to define the function in the .cpp file and use it as needed. However, I've been told that this is not a good idea because the function will be global and may clash with other functions like this etc.

    So what do I do with them, how do I make them more local? Are namespaces the answer? If so, do I make a namespace just for the aux. functions? Or should I wrap each class in its own namespace and put the aux. functions inside it? Or make a namespace for the whole program?

    Thanks for any tips.

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Search for "unnamed namespaces".
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,248
    If the helper function is helping a member function of some object, then chances are this function needs access to other members of that object. In this case, just make it a private member function of the class.

    If the helper function does not need to access private details of the object, then declare and define it as a static function in the .cpp where it will be used. (Or put it in the anonymous namespace, as anon suggested)

    If the helper is used across multiple .cpp files, then it must be globally visible. To discourage outside users from using such an "internal" function, give it a name which indicates this, and do not document it in the user documentation (but certainly document it internally).

    In my own projects, I name cross-module helpers to end with a double underscore. Don't do what a lot of people do and BEGIN the name with an underscore, since all such names are reserved.

    For instance, "LP_BufferInit__", with both the beginning "LP_" and the ending "__" indicating "privateness."

    EDIT: For production libraries, the names can be deliberately stripped from the library or somehow marked as library-level-linkage. But this is overkill for a pet project.
    Last edited by brewbuck; 03-17-2009 at 02:15 PM.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  4. #4
    Registered User draugr's Avatar
    Join Date
    Mar 2009
    Posts
    9
    Thanks!

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    If it's just for one particular class, make it a static free function in the .cpp file. If it has to access members, pass them by reference. If you make it a private member function, client classes that include the header will have to recompile if the signature changes. Not a huge deal, but in large projects it can save time.
    Last edited by medievalelks; 03-17-2009 at 03:33 PM.

  6. #6
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,248
    Quote Originally Posted by medievalelks View Post
    If it's just for that particular class, make it a static free function in the .cpp file. If it has to access members, pass them by reference. If you make it a private member function, client classes that include the header will have to recompile if the signature changes. Not a huge deal, but in large projects it can save time.
    Good recommendation.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  7. #7
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    Quote Originally Posted by brewbuck View Post
    Good recommendation.

    Compliments of Mr. Lakos.


  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,339
    >> If it's just for one particular class, make it a static free function in the .cpp file.

    FYI, static has been deprecated in favor of the unnamed namespace for this type of use.

  9. #9
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    Deprecated as in not supported or not preferred?

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,889
    Quote Originally Posted by brewbuck
    In my own projects, I name cross-module helpers to end with a double underscore. Don't do what a lot of people do and BEGIN the name with an underscore, since all such names are reserved.
    Names that contain double underscores are reserved similiarly to those that begin with an underscore followed by an uppercase letter.

    Quote Originally Posted by medievalelks
    Deprecated as in not supported or not preferred?
    Not preferred.
    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

  11. #11
    Registered User
    Join Date
    Dec 2008
    Location
    Black River
    Posts
    128
    Quote Originally Posted by laserlight View Post
    Names that contain double underscores are reserved similiarly to those that begin with an underscore followed by an uppercase letter.
    I thought it was only names that began with 2 underscores?
    Last edited by Ronix; 03-17-2009 at 09:04 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Calling functions within functions
    By saahmed in forum C Programming
    Replies: 1
    Last Post: 03-09-2006, 01:03 AM
  2. Functions and Classes - What did I do wrong?
    By redmage in forum C++ Programming
    Replies: 5
    Last Post: 04-11-2005, 11:50 AM
  3. calling functions within functions
    By edd1986 in forum C Programming
    Replies: 3
    Last Post: 03-29-2005, 02:35 AM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM
  5. local IP identification via C internal functions
    By andrew in forum C Programming
    Replies: 0
    Last Post: 12-19-2001, 05:46 AM

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