HOWTO use a Factory
Hi all,
i have some questions/problems about "Factories".
My origin is a constructor which needs a string in special format.
An example:
class Example_t {
public:
Example_t(std::string &aString);
...
}
My first idea was to check "aString" IN the constructor with
"boost::regex" AND to throw an exception if "aString" was bad.
Now I know it's bad to throw an exception in a constructor.
But what else?
How can I AVOID elegant a construction of an instance of "Example_t"
if "aString" is in bad format?
I've read a lot at the net but I'm still unhappy. At some other forum
I got the information to use something like that:
class Validator
{
public:
virtual bool validate(std::string) = 0;
};
class A
{
private:
A();
};
class Factory
{
public:
static A createA(std::string, *Validator);
static B createB(std::string, *Validator);
};
But I don't understand how to AVOID a construction with classes above
if the string is in bad format?
Thanks
Goran