Static function without templates in a template class

Firstly, I have no problems, but I like to keep everything as simple as possible. I am using a template with a template named Math, and besides many other things, there are random functions.

Various types of random functions and a function for setting a random seed. Therefore, every function except the seed function uses a type class Real. But when I want to set the seed, I have to pass some random (ha ha) type in order to be able to call the function:

Math<u32>::SeedRandom(System::time());

Again this is not a real problem, but I am curious if it is possible to get the same result without using <u32>.

Here is a snippet from the Math class:

template <class Real>
class Math
{
public:
    static void SeedRandom(u32 seed) { srand(seed); }
    static Real UnitRandom() { return (Real)((f64)rand() / (f64)RAND_MAX); }
};

by the way. f64typedef'd before doubleand u32before unsigned int.

+5
3

ClassName::FunctionName. ClassName , . , .. object.StaticFunctionName, , , , .

, , memember ( , ), , .

+4

Math::SeedRandom ( , , Math ). , , - SeedRandom , , Math<whatever>::SeedRandom.

+2

I am very skeptical of the Math class because it is difficult for me to imagine an instance of Math. From its sounds, you can use the namespace with some free template features. That you have this problem may be a sign that perhaps you should not do what you are doing.

C ++ is a language with several paradigms and, as such, does not force everything to be part of an object, unlike languages ​​such as Java.

0
source

All Articles