Say I have base class Validator
.
class Validator
{
public:
void validate(const string& str)
{
if( k_valid_keys.find(str) == k_valid_keys.end() )
throw exception();
}
private:
const static std::set<string> k_valid_keys;
};
Now assume I need to extend class Validator
. Each derived class will have its own set of valid keys.
My goal is:
- keep
k_valid_keys
a member ofValidator
. No need to add it to each derived classes especially when there are more than a few types of those. - keep
k_valid_keys
static
. Assume I have multiple instances ofValidator
(and its derived classed) and initialization ofk_valid_keys
is expensive.
How can I initialize static
member polymorphically? well, I know that it can't be done (please correct if I'm wrong).
So Assuming it can't be done, any idea of a better design for this problem?