in my c++ dll application I want to define a variable byte, which its value will be initiate in the dll init with random value and then it will hold the same value during the dll live.
- what is the definition for this variable?
- where should I initiate it in code (where is the init of library)
thanks.
Either static class member (accessible from anywhere) or file-scope static or anonymous namespace member (accessible from just that compilation unit).
Static class member:
in .h:
in .cpp:
File-scoped static:
Anonymous namespace member:
In C++ the initializer can be arbitrary expression and the runtime will make sure to run it before
main()function; in case of shared library when it’s loaded. Just remember, that these initialization expressions are called in order of appearance within compilation unit (single .cpp), but initialization from different compilation units will run at random order, so make sure they don’t depend on each other. They can depend on variables initialized to constants being already initialized.