I am trying to write an exception class convinient to use with a constructor behaved like printf, example:
class ExcBase
{
ExcBase(const char *fmt, ...)
{
// call things like vsprintf
}
};
but inheritance of construct does not seem available in c++, so I want write a inherited class like:
class ExcChild : public ExcBase
{
ExcChild(const char *fmt, ...)
: ExcBase(fmt, ...) // XXX: how to pass the trailing parameters to the constructor?
{
}
};
or I will have to write the same constructor for all the child classes, and that was too annoying…
this question troubles me a lot, and I can not figure out a way to solve this…
any information will be a great help…
If you separate out the complicated work into a function that takes a va_list argument, you should be able to call that from each child constructor (which you’d still have to implement for each child type). Then your duplicated code (per class) would just be declaring va_list, then calling va_start, your new (base) function, and va_end. There’s a post here on SO about doing that.