c++ - 关于srand函数的一个疑问
迷茫
迷茫 2017-04-17 13:50:46
0
1
904

在写一个程序时,构造了一个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()么?

还是只能定义全局函数才能作用到。。。

迷茫
迷茫

业精于勤,荒于嬉;行成于思,毁于随。

reply all(1)
Peter_Zhu

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 subsequent rand() calls in the same process.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!