Using a Random class in a static way
I am making a simple Random
class:
class Random
{
public:
static bool seeded = false;
static void SeedRandom( int number )
{
srand(number);
}
static int GetRandom(int low, int high)
{
if ( !seeded )
{
srand ((int)time(NULL));
}
return (rand() % (high - low)) + low;
}
};
Obviously C++ doesn't allow a whole class to be declared as static
(which is what makes this so easy in C#). I've instead made all the members as static
. There is also no static
constructor so I have no way to initialize my bool seeded
unless I call a function by hand, which defeats the purpose. I can instead use a regular constructor in which I would have to create an instance of Random
, which I don't开发者_运维问答 want to do.
Also, does anyone know if the new C++0x standard will allow static classes and/or static constructors?
c++ doesn't allow declaring a whole class as static
Of course it does.
class RandomClass
{
public:
RandomClass()
{
srand(time(0));
}
int NextInt(int high, int low)
{
return (rand() % (high - low)) + low;
}
}
RandomClass Random; //Global variable "Random" has static storage duration
//C# needs to explicitly allow this somehow because C# does not have global variables,
//which is why it allows applying the static keyword to a class. But this is not C#,
//and we have globals here. ;)
Really though, there's no reason to put this in a class. C++ does not force you to put everything in classes -- for good reason. In C# you are forced to put everything into a class and declare things in static methods and such, but that is not ideomatic C++.
You really can't just take ideomatic C# code, and write that in C++, and expect it to work well. They are very different languages with very different requirements and programming characteristics.
If you want an ideomatic C++ way to do this, don't make a class at all. Call srand
inside your main
, and define a function that does your clamping:
int RandomInteger(int high, int low)
{
return (std::rand() % (high - low)) + low;
}
EDIT: Of course, it would be better for you to use the new random number generation facility and uniform_int_distribution
to get your clamped range instead of rand
. See rand()
considered harmful.
Your static bool seeded
will need to be defined in a cpp file anyway, and you must initialise it there.
bool Random::seeded = false;
You don't have to make everything a class in C++.
namespace Random
{
bool seeded = false;
void SeedRandom(int number)
{ srand(number); }
int GetRandom(int low, int high)
{
if (!seeded)
{ srand((int)time(NULL)); }
return (rand() % (high - low)) + low;
}
}
Try this:
class Random
{
public:
static bool seeded;
static void SeedRandom(int number)
{
srand(number);
seeded = true;
}
static int GetRandom(int low, int high)
{
if (!seeded)
SeedRandom(time(0));
return (rand() % (high - low)) + low;
}
};
bool Random::seeded;
static
bool
s get initialized to false
by default, so no need to do so explicitly. Note that your actual class' logic was wrong as well, as you never set seeded
to true
.
You could always approach this with a singleton pattern, by having the constructor private and a static accessor for the object.
class Random
{
public:
static Random& instance()
{
static Random instance;
return instance;
}
// your other functions here
private:
Random()
{
// your seed code here
}
};
This will ensure you only have a single instance of the class and whenever you need a random you just need to call Random::instance().function()
What is the reason for you to have a class with static methods? May be there is an alternate to achieve your goal. Are you looking for something SIMILAR to singleton?
BTW if you have static members variables in your class declaration, as pointed out by other answers before me, you must initialize them outside the class (preferably in the cpp file) before you use them. So if you follow this and initialize your like this:
bool Random::seeded = false
it will be automatically initialized to false as you want.
精彩评论