Hi ,
I have a strange problem. Currently i am using array of structures and initializing it statically at compile time.
e.g.
const MyStruct elements[] = {
{"abc",VALUE1,5}
{"def",VALUE2,15}
};
MyStruct is a structure with three members.
This way I am able to initialize the "elements" array at compile time.
Now i want the same thing to be achieved by using either vector or map.I know that static initialization will not be possible for vector and map.
But how to do the initialization if there are suppose 100 values to be initialized in "elements".
I can call this function in one of the main class constructor.
const map<Test_list,Test_info> Test_info_map = Create_Test_info_map();
But i want to keep the Unit_info_map variable global. And calling the above statement in global scope seems a bit ugly to me.
If it has to be global, it has to be global. One possible improvement might be to wrap the map in a class, initialize it in its constructor, and make a global instance. That might keep things a little more organized but that's about it.
But compiler gives error in for loop statement that "=" operator is not defined. THis for loop is being executed in another class's member function. I assume this should not be a problem as Test_table1 is global.
Also, global variables in header files are doubly bad. Every .cpp file that includes that header,
directly or indirectly, will get its own copy of the data which is both not what you want and
a memory hog.
I found out the issue.Actually because Test_table1 is const i have to use const_iterator instead of normal iterator.
Also i can declare the Test_data[] as static so that only one copy of it is available. Will that be right soln?
If you declare a global variable (not a class member) as static, it will have module-scope.
jsmith wrote:
Also, global variables in header files are doubly bad. Every .cpp file that includes that header,
directly or indirectly, will get its own copy of the data which is both not what you want and
a memory hog.
Actually I think it'll be a multiple definition error once it's included more than once.
I think what you should do, if you want to share the data across files, is to create a source file that contains the global. Then use the extern keyword in a header that will be included elsewhere.