This entry was posted on August 30, 2006 at 12:01 pm and is filed under C/C++. You can follow any responses to this entry through the RSS 2.0 feed.
You can leave a response, or trackback from your own site.
Thanks i needed that. Not sure if anyone’s stated this but would only really work uniquely if you keep your integers between -127 and 127, otherwise you will not get a unique string, ie char only goes from -127 to 127 as it is one byte, whereas integer i believe is 2 bytes and goes 0 to 65535 or -32767 to 32767.
I believe if you put in integer higher than 127 it just loops round to -127 and continues.
i believe its not limited to char type size since the stream takes each number in at a time, and each number is a char like it would read 512 as “(char)53 << (char)49 << (char)50", so dont have to worry about the size of the int or whatever.
Can this be used in a function? I’ve seen a claim that it can, as in
but isn’t that returning a (plain old, dumb) pointer to memory that is going out of scope?
I’m storing a bunch of different data from a structure into something like an XML DOM, all string data. For now I have a bunch of overloads
SaveData(whereToPutIt, int iValue);
SaveData(whereToPutIt, double dValue);
SaveData(whereToPutIt, char * szValue);
so I can call
The overload for int does the conversion as above and calls the string overload. I’d like to concentrate all the conversions like
SaveData(whereToPutIt, ToStr(MyStructure.dataField) );
and not have all the overloads, because I think it would be more elegant, but I want neither of smart pointers (outside the magnitude of the task I’ve been given), pointers to orphans, or memory leaks. And some global buffer with a macro might make my employer discover that I’m a really a C programmer, not an OO C++ programmer.