CString and C++, not so easy to understand

Well, I was trying to please VS2013 compiler, when I began to try this code:

CString::CString(double pDouble) : buffer(0), buffc(0), sizec(0), readc(0), writec(0) { char tempBuff[32]; sprintf_s(tempBuff, 32, "%f", pDouble); *this = tempBuff; }
Ok, it was sprintf_s(tempBuff, “%f”, pDouble); before I changed it to sprintf_s …

Anyway, this looks very weird to me.
If I look at the end of CString class in the .h file:

[CODE] inline unsigned int readGUInt4();
inline unsigned int readGUInt5();

protected:
	char *buffer;
	int buffc, sizec, readc, writec;

};[/CODE] I can begin to guess that this points to the CString Object, *this is said to be not the object but a copy of it according to: http://stackoverflow.com/questions/2750316/this-vs-this-in-c and I can guess that (*this).buffer is equivalent to *this because buffer is the first defined thing in CString object… but where I have problem… from C is tempbuffer defined on the stack? Then how come we use that like if it was a dynamic allocated region like malloc gives? I would expect that memory to disappear from memory at any point.

Return *this returns a copy. The code you posted doesnt return anything. I really cant say more because I have no fucking clue what you are even trying to ask.

Edit: it looks like you are performing an operation on that dereferenced pointer. So you are actually editing the current object instead of making a copy.

The current code is:

CString::CString(double pDouble) : buffer(0), buffc(0), sizec(0), readc(0), writec(0) { char tempBuff[32]; sprintf(tempBuff, "%f", pDouble); *this = tempBuff; } but I would have written:

CString::CString(double pDouble) : buffc(0), sizec(0), readc(0), writec(0) { char tempBuff[32]; sprintf_s(tempBuff, 32, "%f", pDouble); clear(strlen(tempBuff); strncpy_s(buffer, 32, tempBuff, strlen(tempBuffer)); } See https://github.com/OpenGraal/GServer-v2/blob/master/server/src/CString.cpp and https://github.com/OpenGraal/GServer-v2/blob/master/server/include/CString.h

I am not convinced the original code is correct. It looks like it try to make buffer points to a temporary value, which seems wrong. That said, I am pretty newbie on C++.