I want to make some functions that I expect I will use again and again, but I don't want to copy and paste them everytime I make a program that will use them. My thought was to make a .h file with a namespace. Would that work? Or would I have to make a .dll? If so, how would I go about doing that? Thank you very much.

Recommended Answers

All 5 Replies

> My thought was to make a .h file with a namespace. Would that work?
yes, provided that all the functions in the header have internal linkage. ie. they are inline or put in an anonymous namespace (or have the deprecated static linkage specifier). and you have an include guard for your header file.

> Or would I have to make a .dll?
if you have a header and seperate implementation files (.c .cc .cpp), you would need to make a library of some kind. either a static library or a shared library (dll).

> If so, how would I go about doing that?
depends on the toolset (microsoft/gnu/borland etc) that you are using.

> My thought was to make a .h file with a namespace. Would that work?
yes, provided that all the functions in the header have internal linkage. ie. they are inline or put in an anonymous namespace (or have the deprecated static linkage specifier). and you have an include guard for your header file.

What do you mean by the functions having an internal linkage? Wouldn't "using namespace whatever;" work?

Also, from my understanding, an anonymous namespace is just one without a name, so how would I call that?

The guard would be so I don't call unwanted functions, right? But isn't that implied of an anonymous namespace?

I'm planning on keeping everything in one source file, but in case it helps, I'm using Dev-C++ 4.9.9.2.

>>What do you mean by the functions having an internal linkage?

Explaination here:

>>Also, from my understanding, an anonymous namespace is just one without a name, so how would I call that?
The same way you do all other functions in the standard C/C++ libraries (other than class libraries). None of them are defined within a namespace. Just don't declare a namespace for them.


>>The guard would be so I don't call unwanted functions, right? But isn't that implied of an anonymous namespace?

No. Code gards are a completly different issue, and has nothing to do with namespaces.

> I'm planning on keeping everything in one source file
the file would be a header of this kind.

#ifndef _MY_LIBRARY_H_INCLUDED_ // include guard
#define _MY_LIBRARY_H_INCLUDED_ // include guard

#include <sstream>
#include <string>
#include <cstdlib>

namespace my_library
{
   // inline => internal linkage
   inline int digit( int number, std::size_t pos ) ;

   // const => internal linkage
   const int TEN = 10 ;

   // anonymous namespace => internal linkage
   namespace
   {
      int thousand = 1000 ;
      int sum_digits( int number ) ;
   }

   // static (deprecated) => internal linkage
   static int hundred = 100 ;
   static int product_digits( int number ) ;
}

//////////////////////////////////////////////////////
//                implementation
// may be in another file (say my_library.inc)
// which is #included here
// #include my_library.inc
//////////////////////////////////////////////////////

namespace my_library
{
   int digit( int number, std::size_t pos )
   {
     std::ostringstream stm ;
     stm << std::abs(number) ;
     const std::string& str = stm.str() ;
     if( pos < str.size() )
       return str[ str.size()-pos - 1 ] - '0' ;
     return -1 ;
   }

   namespace
   {
      int sum_digits( int number )
      {
        int sum = 0 ;
        int d = digit( number, 0 ) ;
        for( int i=0 ; d != -1 ; d = digit(number,++i) )
          sum += d ;
        return sum ;
      }
   }

   int product_digits( int number )
   {
        int prod = 1 ;
        int d = digit( number, 0 ) ;
        for( int i=0 ; d != -1 ; d = digit(number,++i) )
        {
          prod *= d ;
          if( prod == 0 ) break ;
        }
        return prod ;
   }
}

#endif // _MY_LIBRARY_H_INCLUDED_

you would use it this way:

#include "my_library.h"
#include <iostream>

int main()
{
  using namespace my_library ;
  int i = 123456 + TEN + hundred + thousand ;
  std::cout << i << ' '
            << sum_digits(i) << ' '
            << product_digits(i) << '\n' ;
}

OK, I think I understand now. Thank you SO much for helping me out on this.:):):):)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.