Let's say I have a template base class like this:
class Util {
public:
Util(const std::string& suffix) : name(base_name + "." + suffix) {}
protected:
const std::string base_name = "Util";
const std::string name;
};
and I want to create a number of subclasses with different names.
I could do it like this:
class WorkingUtil : public Util {
public:
WorkingUtil() : Util("Working") {}
};
But not like this:
class Bad1Util : public Util {
public:
Bad1Util() : Util(suffix) {}
private:
const std::string suffix = "Bad1";
};
nor this:
class Bad2Util : public Util {
public:
Bad2Util() : suffix("Bad2"), Util(suffix) {}
private:
const std::string suffix;
};
Here is a godbolt example and the error:
terminate called after throwing an instance of 'std::bad_alloc'
what(): std::bad_alloc
What exactly is the problem here? I could do it like the top example, but I want to know why the other ones are not acceptable.