The Case of the Missing Object
I'm having a problem with an object suddenly disappearing on me.
Suppose I have this class
class Foo {
typedef std::map<std::vector<int>, int> LookupTable;
mutable std::map<size_t, LookupTable> tablesForSize;
void evaluate(...) const;
};
void Foo::evaluate(...) {
...
size_t tableKey = ...;
LookupTable table = tablesForSize(tableKey);
....
std::vector<int> innerTableKey = ...;
if (table.count(innerTableKey) == 0) {
table[innerTableKey] = new_unique_value;
}
return table[innerTableKey];
}
In evaluate(), the function is supposed to come up with a size_t
(tableKey) based on its input, then return the corresponding
LookupTable (table) from tablesForSize. This function also comes up
with a vector<int> (innerTableKey) based on its input, and the
function returns the value corresponding to the vector<int> from table
(or if absent, returns a new value; each LookupTable is basically
built up over multiple calls to evaluate()). The problem is as
follows: suppose that evaluate() is being called twice in succession
(eg. in a loop). In the second evaluate(), the tableKey that's
computed is the same is the one computed from the first call, and thus
the table retrieved is the same table as from the first evaluate().
However, for some unknown reason, table is now blank, instead of
having the entry created from the previous call to evaluate()! What
happened to the entry, and what should I do to get it back?
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]