Thread: Namespaces

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    3

    Namespaces

    Hi,

    My problem is relatively simple, but I am pretty confused as to how to implement a solution...

    My project will be using random numbers across various classes. I currently have a class which creates some pretty sweet randoms. What I want to do is be able to access a call to the random number generator anywhere in my program - within any class that might need it.

    On another board people suggested using a namespace - which i will freely admit is not something I totally understand. I get the basic concept, but I don't see quite how implement.

    Currently I have the following files:

    main.cpp
    yardage.h (this is for a football game - these are yardage results)
    yardage.cpp
    mersenne.h (rng header)
    mersenne.cpp (rng implement)
    untilites.h (this is where the namespace would live)

    I will be generating randoms in both Main and yardage (among others)

    The thing is, the mersenne class is pretty complex and I'm not sure how to approach implementing this.

    Any suggestions? Please remember - I'm (apparently) pretty noobish here, so as much patience as you can muster would be appreciated.

  2. #2
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Namespaces exist in C++ basically to avoid naming clashes. For example, if I wanted to create my own string class, I could put it in a namespace (since it's very likely that other classes of name "string" exist):
    Code:
    namespace JaWiB
    {
      class string
     {
     //...
     };
    }
    int main()
    {
      JaWiB::string jStr; //use my string
      std::string stdStr; //use a std::string
    }
    I'm not experienced with prng's but perhaps you could make a static member function in the class? Eg:
    Code:
    class PRNG
    {
    public:
      static void srand(unsigned seed);
      static int rand();
    };
    int main()
    {
      PRNG::srand(someseed);
      int num =  PRNG::rand();
    }
    This is similar to using namespace, but the functions are in a class so you don't have to worry about clashes.
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. need help wiht using classes...namespaces...
    By hockey97 in forum C++ Programming
    Replies: 9
    Last Post: 09-02-2008, 02:22 PM
  2. namespaces
    By Ancient Dragon in forum C++ Programming
    Replies: 6
    Last Post: 09-30-2005, 06:04 PM
  3. Are namespaces a good idea?
    By Crilston in forum Game Programming
    Replies: 6
    Last Post: 06-24-2005, 06:30 PM
  4. Namespaces
    By the pooper in forum C Programming
    Replies: 8
    Last Post: 01-21-2005, 09:06 AM
  5. C and namespaces
    By oyse in forum C Programming
    Replies: 2
    Last Post: 05-05-2004, 09:39 AM