I have a series of classes that tells the debug stream (std::cout in this case) that it has been created, allowing me to follow the program execution nicely. I have several classes that are subclasses of base classes which are not abstract, which results in a double message when a subclass instance is created. I would like to suppress the output in the base class constructor when it is called from a subclass. I know this probably isn’t possible without some fancy trick, if it is even possible at all.
I did think of using the backspace escape sequence \b, and doing just enough of that to delete the previous message not really efficient, but it’s debug info, performance isn’t that critical then…). I’m not sure of the portability or effectiveness of this approach.
Any ideas are welcome, thanks for the effort!
There’s no way to suppress the code in the base constructor, unless the code in the base constructor checks some condition itself. You may achieve this by e.g. passing a special flag to the base constructor (having default value NOT prohibiting the debug output).
Outputting
\b‘s won’t help if your output is redirected to a file etc.A decent solution could be to create a virtual function which returns a string, and output the result of that function. This won’t however work for your case (calling from constructor), as during the base constructor run the virtual functions behave as if the instance is of the base type, not derived.