I’m not too sure how to ask this question. I need to store a ton of data into a vector at the beginning of the program, but only once. I tried putting that vector, my_vect, inside a constructor in a base class to load all the data into it, but the problem is, that data needs to be used by the derived classes that will be created throughout the program.
Essentially (unless I took a totally different approach?) the vector needs to be static because I don’t want to have to reload the data into the vector every time a child class is created. Can I somehow create this my_vect globally to avoid this static singleton stuff? I also need to be able to iterate through my_vect extremely fast in my derived classes. Is putting extern in every file a good choice? Please forgive me for any convoluted-ness, and say if you need any clarification.
EDIT:
Heres the jist of my program. I have a vector that reads thousands of lines from text file and stores them by words. No prob. I need to do that at the start of my program and I only want to store them once. After they are stored, every class I have needs to have fast my_vect[incr] access to the vector. I tried putting that vector as protected inside a base class so its initialized from the start, but the problem was that every time a new derived class object is created it would be loading all the data into the vector again. I only want the vector to be stored once, and used everywhere. Maybe singletons arn’t the way to go?
Why do you need to inherit from the singleton to use the data it provides? You can make a singleton as the data provider, and make the other classes use it to access the data.
In fact, you don’t even need a singleton. Just make an ordinary class with a static field and a static method to initialize the field. Then call the static method from
main()before any of the threads are started to simplify the code. For the rest, since the class has no data, you can instantiate it at a tiny cost, and use it as you would any other class.