在写一个程序时,构造了一个Cache类,然后再Cache类的构造函数中,写了这样一段代码:
enum replacement_way { NONE, FIFO, LRU, RAND };
class Cache {
private:
replacement_way ReplcWay;
……
public:
Cache(){
……
switch (ReplcWay)
{
case FIFO:
SelectListInit();
break;
case LRU:
SelectQueInit();
break;
case RAND:
srand((unsigned int)time(NULL));
break;
default:
break;
}
……
}
现在我有一个疑问,就是srand((unsigned int)time(NULL));能作用到同一个Cache的其他public/private函数中的rand()么?
还是只能定义全局函数才能作用到。。。
This problem has nothing to do with the constructor of the class.
srand
is a global function. No matter where you call it, it will have the same impact on all subsequentrand()
calls in the same process.