Victor Bazarov said:
You're assigning to it, aren't you? And you want the assignment to
stick, don't you? So, returning a temporary is OK if you don't care
that the value you assign isn't stored in the map itself and basically
immediately forgotten after the temporary goes away.
Uhm well I tried with the following
string& operator[](const string& key) { return conf[key]; }
string operator[](string& key) { return conf[key]; }
string operator[](const string& key) { return conf[key]; }
and in all the three cases it works in the same way, if I assign
something I also find it after...
Yes, or you could simply have a static dummy object that would
initialize your map, like so:
int dummy = GLOBALS_initializer_function(); // namespace scope
And fill up your GLOBALS map in that function... Of course, there is
always the static object initialization fiasco you need to worry
about. Make sure no static objects ever use your GLOBALS before it's
constructed (in case you go with the class) or filled in (if you go
with the initialization function).
V
Interesting the fiasco, but well a static object will always call a
constructor before going further?
Or should I just have another function "initialize()" and make sure it's
called before I start to read?
Globals.h:
extern Globals CONF;
class Globals
{
Globals::Globals() {...}
};
and in Globals.cpp there is
Globals CONF;
Would that be already enough to avoid the fiasco?
Or as they say on the FAQ should I create a function which creates this
global object (not so important if I can't clear it)??
And now instead another problem, supposing I have the following
parameters.
--8<---------------cut here---------------start------------->8---
int x;
long y;
str::vector<string> z;
--8<---------------cut here---------------end--------------->8---
the idea was to use a mapping to avoid repetition in the assignment from
the ini file, so that I can just do
CONFIG["x"] = 100;
And use a for loop on the map.
BUT I only read strings in the ini file, and I have to convert the
values somehow.
So the best thing would be to have a something like
{ x : (value, function),
y : (value, function)
}
and then call the corresponding function.
Or what other data structure would be good?
How can I do that? Maybe a function pointer, like:
{ x : ("0", &atoi), ...}
which then calls the right function before actually assigning something.
Or how else can I do to accomplish this?
PS. use boost unfortunately is not an answer since I can't (at least for
this part of the project