Hey, I'm developing using Allegro, and it uses C-style strings, which is starting to become a pain. For example, I have code such as:
addMessage("%s %d", getName().c_str(), getAge());
addMessage is just a printf kind of function that uses variable arguments to create a string that will go to Allegro's textout, which takes a char * among other things. This works fine, but what I'd rather have is something like:
addMessage << getName() << " " << getAge();
So, I really just need to do something with strings and streams. Could someone help me out with what specifically needs to be done to implement this?
>>2
That looks like the right direction, and I'm thankful for your help. My question seemed a little incorrect from being written so late at night, though. I have an addMessage function that extracts the variable char * arguments given to it, and then it does:
messageQueue.push_back(newMessage);
messageQueue is just a list of strings. So, I don't really need to worry about the Allegro part at this point. Instead, I'd like to implement some kind of
foo << "---" << "etc";
foo needs to be able to push these things to the messageQueue, but with only one string per function call. Now that I think of it, is that even possible with streams?
Name:
Anonymous2007-03-23 12:57 ID:Xi9exePw
Considering that << is left associative, it's definitely possible. You'll want to look at cin's signature to get an idea how it's done. Something along the lines of overriding <<, accepting char * as an argument, and returning a copy of your object so that the next << can operate it.
Name:
Anonymous2007-03-23 12:58 ID:Xi9exePw
Actually, doesn't even need to be a copy.
Name:
Anonymous2007-03-23 13:05 ID:/S/tBbjI
>>7
So, it looks like I'll have to override char *, int, and whatever else I want it to handle automatically. That's fine, but I'm still not sure on how exactly to do it. Sure, I could just pass a stringstream to a function, but then I'd have something like:
So, all I need is an instance of the message handler (or make them all static), and of course add ints and whatever else I want to have.
One more question, though. Why is it that I have to use ostringstream::str("") to clear its string? It seems like ostringstream::clear() doesn't do shit.
Name:
Anonymous2007-03-23 14:12 ID:oERjl27T
man, you gotta be a phd to do strings in C/C++
Name:
Anonymous2007-03-23 15:59 ID:qUWusmUp
>>13
What's an easier language for it? And don't say Scheme.
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
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 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 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