well, ok, i could use std::strings, but printf won't accept them, so i
have to convert them where ever i want to use them, right?
and i don't really like all these assumptions about me being new to
C++. yes, i understand where that's coming from, but i've used C++ for
a couple years, unfortunately without any proper instruction.
i've learnt the basics in school, and the rest i've taught myself.
needless to say, the teacher never really covered pointers and such.
i've mostly spent my time teaching myself game design, on a need to
know basis. when i run into a problem, i run out and find the
solution, and thus i'm constantly learning.
i just completed my first semester in university... in which i had to
learn Java. i wasn't keen on it at first, thinking C++ was
superior..but grudgingly i think i gave into some of Java's simplicity
of string handling.
i've always tried to advoid std:strings and other libraries i didn't
program myself... always thinking there is too much baggage, or they
aren't compatible with other functions and such.
anyways... that's my story.
http://groups.google.com/group/comp.lang.c++/msg/6f4ab6951b54236d
so.. by making it static, the buffer data is sort of "locked" in
memory, and thus it doesnt get overwritten or lost when the function
returns. thus our pointer will still point to meaningful data,
correct?
running this quick test,
#include <cstdlib>
#include <iostream>
using namespace std;
char * itoa(int i)
{
static char buff[35];
return _itoa(i,buff,10);
}
int main(int argc, char *argv[])
{
printf("%s %s %s", itoa(1), itoa(2), itoa(3));
system("PAUSE");
return EXIT_SUCCESS;
}
it prints "1 1 1"
now if i'm not mistaken, since the buffer is in the same memory
location each time the function is called (because it's static)... all
3 pointers should point to the same location in memory, which is
probably why they are printing the same thing.
now what i'm thinking really happens, is that itoa is called 3 times,
the pointers are returned, and THEN the "%s %s %s" is evaluated, using
what happens to be the same pointer, rather than evaluating each %s and
corresponding parameter at the same time....
which all makes very good sense to me now. i feel clever for figuring
that out .. i hope i'm right
lol.
so i'm thinking
http://groups.google.com/group/comp.lang.c++/msg/df3e00fcce160384 would
be my best solution? (xitoa)
are there any other print functions that actually take std:strings as
parameters?
or is there a way i could do "my string" + 5 + my_other_string?