c++ - How to create a wrapper on printf? -
this question has answer here:
- call printf using va_list 4 answers
i want write conditional printf, this
class conditionalprintf { public: conditionalprintf(bool print) : print_(print) {} void printf(int x, double y, char b, const char* format, ...) const { // use x, y , b va_list argptr; va_start(argptr, format); if (print_) printf(format, argptr); va_end(argptr); } private: bool print_; };
but prints garbage. there wrong? may implicit parameter change things?
also, if isn't idea whatsoever, other solutions there? don't want write if (print) printf(...)
billion times.
vprintf
forwards arg list printf
#include <stdio.h> #include <stdarg.h> class conditionalprintf { public: conditionalprintf(bool print) : print_(print) {} void printf(int x, double y, char b, const char* format, ...) const { // use x, y , b va_list argptr; va_start(argptr, format); if (print_) vprintf(format, argptr); va_end(argptr); } private: bool print_; };
Comments
Post a Comment