Initialization of object static members

前端 未结 7 1054
余生分开走
余生分开走 2021-01-24 21:51

Static members confuse me sometimes. I understand how to initialize a simple built in type such as int with something along the lines of int myClass::statVar

7条回答
  •  鱼传尺愫
    2021-01-24 22:17

    Just write a function which returns a reference to a properly randomized RandomGenerator and turn itsGenerator into a reference to a generator:

    class myClass
    {
    public:
     // Some methods...
    
    protected:
     // make this a reference to the real generator
     static RandomGenerator& itsGenerator;
    public:
     static RandomGenerator& make_a_generator() 
     {
       RandomGenerator *g=0;
        g=new RandomGenerator();
        g->Randomize();
       return *g;
     }
    }
    
    RandomGenerator& myClass::itsGenerator=myClass::make_a_generator();
    

提交回复
热议问题