c++ - How to create a wrapper on printf? -


this question has answer here:

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

Popular posts from this blog

sequelize.js - Sequelize group by with association includes id -

delphi - Take screenshot in webcam using VFrames in Console Application -

c++ - Migration from QScriptEngine to QJSEngine -