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?



However you can write something like

 std::string s;
 s.assign(strlen("Hello world"), '\0');
 strcpy(&s[0], "Hello world");


On Jan 25, 2008 7:26 AM, Matthew Weigel <unique@idempot.net> wrote:
> 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
>
>
>

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