J
Jojo
I have a code like (just some illustrative lines, many things are omitted):
class string_interpreter {
static std::vector<* string_interpreter> my_vector;
*string_interpreter init(const string& s);
... // There's an "operator =" also, and ctor, dtor.
};
*string_interpreter string_interpreter::init(const string& s)
{
// Look in the vector if string 's' was already interpreted.
for (int i = 0; i < vector.length(); ++i)
if (//string 's' is already interpreted)
return vector; // return the result of the interpretation
// Not in the vector, do string interpretation. Store results in the
// class's properties.
...
// Save results in the vector and return.
string_interpreter *new_interpreted_string = new string_interpreter;
*new_interpreted_string = *this;
my_vector.push_back(new_interpreted_string);
return new_interpreted_string;
}
So, basically this class accepts a string, interprets it and stores the
results in its own properties. Then the class makes sure that every
interpreted string (and its results, all within the class itself) is
stored in a vector to ensure that a string needs be interpreted only once.
The reason why I store pointers in the vector and not the objects
itself, is that these pointers are accepted by other classes/modules
which also may store those pointers and have immediate access to the
interpretation results.
My questions:
* I think the code I have is safe with respect to the "static
initialization order fiasco" because the static vector is within the
class itself. Or am I wrong?
* There does not seem to be a way to delete all class
string_interpreter objects from memory if the program terminates. Is
that bad programming? Or is it normal to depend on the dtor of the
vector to delete all the objects in the list?
* Any other bad programming things here?
Thanks,
Jeroen
class string_interpreter {
static std::vector<* string_interpreter> my_vector;
*string_interpreter init(const string& s);
... // There's an "operator =" also, and ctor, dtor.
};
*string_interpreter string_interpreter::init(const string& s)
{
// Look in the vector if string 's' was already interpreted.
for (int i = 0; i < vector.length(); ++i)
if (//string 's' is already interpreted)
return vector; // return the result of the interpretation
// Not in the vector, do string interpretation. Store results in the
// class's properties.
...
// Save results in the vector and return.
string_interpreter *new_interpreted_string = new string_interpreter;
*new_interpreted_string = *this;
my_vector.push_back(new_interpreted_string);
return new_interpreted_string;
}
So, basically this class accepts a string, interprets it and stores the
results in its own properties. Then the class makes sure that every
interpreted string (and its results, all within the class itself) is
stored in a vector to ensure that a string needs be interpreted only once.
The reason why I store pointers in the vector and not the objects
itself, is that these pointers are accepted by other classes/modules
which also may store those pointers and have immediate access to the
interpretation results.
My questions:
* I think the code I have is safe with respect to the "static
initialization order fiasco" because the static vector is within the
class itself. Or am I wrong?
* There does not seem to be a way to delete all class
string_interpreter objects from memory if the program terminates. Is
that bad programming? Or is it normal to depend on the dtor of the
vector to delete all the objects in the list?
* Any other bad programming things here?
Thanks,
Jeroen