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

Pages: 1-

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 20:28

the insertion operator takes shit and pumps it into a stream.  it doesn't really understand what you mean by "the formatted string" because it's a stream operator.  it only knows the value you've passed it.

so, for example:
a << "hello " << "world" << someVariable << endl;

would call the stream operator four times, the first time it would be puming "hello " into a, the second time it would be pumping "world", the third time it would be pumping someVariable, and the fourth time it would be pumping endl.  each occurrence is distinct from the rest.

so, at best, you'd be able to call print("hello "), print("world") and so on.

Name: Anonymous 2007-01-15 20:34

ah, i see. so, how would I be able to have something like printf as a class method? ie. formatting a string with variables and passing it to the method, all within in the paramater list.

also, okay if i overloaded operator <<, could i then just have everything saved to some string, then when endl is reached, i call print with the formatted string?

sort of like:
a << "hello"; // m_format += hello;
a << "world"; // m_format += world;
a<<endl; // Print(m_format);

would this work or am i horribly mistaken?

Name: Anonymous 2007-01-15 21:09

>also, okay if i overloaded operator <<, could i then just have everything saved to some string, then when endl is reached, i call print with the formatted string?

yeah, that's doable, but seems pretty ugly.  why not just make print() take a stream instead of a string?

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;
}

Name: Anonymous 2007-01-15 22:02

>>5

oh, I didn't know it was possible to use ellipses inside class methods. i recall one time trying to do it but got compiler errors. i tried it in vc8 and it works. i cant believe it.

Name: Anonymous 2009-01-14 14:30

GTFO

Name: Anonymous 2009-03-06 7:14

A=wsh createobject msxml2 xml   while 1 a   gentle introduction to   programming a modern   shmup with 3D   models 3D backgrounds   scripting language and   a graphics library   For now I   see properly implemented   static typing as   a HUGE improvement   over any kind   of project as   long as we   can complete it   in the alloted   time What are   we going to   spam them to   edit and kill   all toolbars Add   some simple formulas   and poof you   have your application   Serious software companies   work this way   You can either.

Name: Anonymous 2009-08-16 23:23

Lain.

Name: Anonymous 2010-11-15 3:24

Name: Anonymous 2010-12-20 16:41

Name: Anonymous 2012-03-23 23:33

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boyAll work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boyAll work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boyAll work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boyAll work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boyAll work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boyAll work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

All work and no play makes Jack a dull boy

Name: Sgt.Kabu民쀕kiman㿷嘸 2012-05-28 21:48

Bringing /prog/ back to its people
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy

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