search

Home  >  Q&A  >  body text

c++ - 关于srand函数的一个疑问

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

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

迷茫迷茫2767 days ago931

reply all(1)I'll reply

  • 天蓬老师

    天蓬老师2017-04-17 13:52:46

    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.

    reply
    0
  • Cancelreply