Consider the following two classes:
class Parent
{
private:
char outputBuffer[100];
protected:
virtual void WriteOutput() = 0;
};
class Child : public Parent
{
private:
double value1;
double value2;
void WriteOutput()
{
sprintf(outputBuffer, "%.2f %.2f", value1, value2);
}
};
The parent defines a virtual
function that is then implemented in the child, the purpose of which is to write a formatted string to an output buffer.
My challenge is that I would prefer that the output buffer not be directly visible to the child class. In other words, I want WriteOutput to look like:
void WriteOutput()
{
myFcn("%.2f %.2f", value1, value2);
}
But then what does myFcn()
look like? It should just relay to sprintf but the syntax needed to support the varags in sprintf is confusing me. Is it possible to relay a variable number of arguments? I'm showing two values in the example but different children will have different number of values so I have to preserve the vararg capability of sprintf. Note that performance is important here so I can't have unnecessary string copies.
If there's a better / more efficient alternative to sprintf in this context I can consider it. Thanks.