-
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.
-
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.