Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

C++ help

Name: Anonymous 2007-01-15 20:15

hi /prog/

suppose I had a class

class ABC
{
public:
    ABC() {}
    void print(std::string s) { // do something with s }
};

and in my program I wanted to do:

ABC a;
a << "hello " << "world" << someVariable << endl;
but I wanted the << operator to call print, passing the formatted string as s.
how would i do this?



Name: Anonymous 2007-01-15 21:32

>>3
|so, how would I be able to have something like printf as a class method?

#include <cstdarg>
#include <cstdio>

class ABC {
public:
  ABC() {}
  void print(const char* blah, ...) {
    va_list ap;
    char buf[128];
    va_start(ap, blah);
    vsprintf(buf, blah, ap);
    va_end(ap);
    printf(buf);
  }
};

int main() {
  ABC a;
  a.print("%s %s\n", "hello", "world");
  return 0;
}

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List