This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

namespace questions


I'm creating a namespace for a bunch of related typedefs and constants and the following questions came up:

1. What's the meaning of "static" when used in a namespace?
2. What's the difference between "const" and "extern const" when used in a namespace?
3. Are there any tricks/best practices to know when using a namespace (defined in its own header) in multiple source files?


Thanks,

R.

// g++ -Wall namespace.cc main.cc

// The following code can also be compiled as-is in one file.

////// namespace.hh   //////
#ifndef NAMESPACE_HH
#define NAMESPACE_HH

namespace Name
{
  static const char charA = 'A';
  const char charB = 'B';
  extern const char charC;
  extern char charD;
};

#endif // NAMESPACE_HH


////// namespace.cc ////// //#include "namespace.hh"

const char Name::charC = 'C';
char Name::charD = 'D';


////// main.cc ////// #include <stdio.h> //#include "namespace.hh"

int main()
{
  printf("charA = %c\n", Name::charA);
  printf("charB = %c\n", Name::charB);
  printf("charC = %c\n", Name::charC);
  printf("charD = %c\n", Name::charD);

return(0);
}

Attachment: PGP.sig
Description: This is a digitally signed message part


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]