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

android - Basic camera in app -

javascript - Why Selenium can't find an element that is graphically visible -

scala - Using the kronecker product on complex matrices with scalaNLP breeze -