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

ios - Is 'init' forbidden as *part* of a variable name? -

angular - Angular2 Router: Cannot find primary outlet to load 'HomeComponent' -

qt - Microsoft FTP Service 451 The parameter is incorrect -