http://GameProgrammer.Com

Programming

GP Mailing List
     Thread Index
     Date Index

ATXGPSIG List
     Thread Index
     Date Index

Google
>

Home

Wise2Food



[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[gameprogrammer] Re: Is this valid?



Kevin Jenkins wrote:
std::string s;
s.reserve(strlen("Hello world"));
strcpy(s.c_str(), "Hello world");

That makes me cry in so many ways.

1. The signature for string.c_str() is "const charT* c_str() const" so assigning it a value is a bad thing - and not valid. 2. Please, never use strcpy() - even if it's with a constant string. Use strncpy(), and understand its semantics. 3. Why are you mixing C and C++ string manipulation? You are trying to use your std::string as a char*, why not just use a char*?
4. Alternatively, why not use
	std::string s("Hello world");
   to assign the string?
--
 Matthew Weigel
 hacker
 unique & idempot.ent

---------------------
To unsubscribe go to http://gameprogrammer.com/mailinglist.html