Re: std::string to char*

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



"Boni" <nospam@xxxxxxxxxxxx> wrote in message
news:eOtRXwOSGHA.1728@xxxxxxxxxxxxxxxxxxxxxxx
const char *foo2 = foo.c_str();
Is there a reason why not do const_cast?
const char *foo2 = const_cast<char*>(foo.c_str());

Um, because it is an ugly hack? :-)

Seriously, if you could write

std::string str="hello";

char *q = const_cast<char *>( str.c_str() );

*q = 'B';

where would that leave str? Suppose you used that non-const pointer as a
target for a string copy operation where the source length exceeds the
allocation of the string? Nothing good can come of that kind of hackery.

Regards,
Will




.



Relevant Pages

  • Re: A question about fscanf and feof !
    ... But the last string in the input file always printed twice, ... the reason and how can I make the last string be printed only once? ... fscanf(fp, "%s", str); ...
    (comp.lang.c)
  • Re: Question regarding design of the String Class
    ... returning the code of position i in str? ... The reason I ask this is that in other languages strreturns the string starting at position i. ... It seemed strange that copying ranges of strings uses the same format as C ) but not when copying the remainder. ...
    (comp.lang.ruby)
  • Re: Type casting
    ... It's an empty string, ... reason I do that is because I often do: ... If `$str' is not defined before the first iteration, ...
    (comp.lang.php)
  • Question regarding design of the String Class
    ... Was there a reason the string class was implemented with strreturning the code of position i in str? ... The reason I ask this is that in other languages strreturns the string starting at position i. ... It seemed strange that copying ranges of strings uses the same format as C ) but not when copying the remainder. ...
    (comp.lang.ruby)
  • Re: Controlling Javascript from server side
    ... but five different language implementations here. ... 'true' means that the request must be handled asynchronously. ... There is exactly *no* reason for such a thing here. ... | percent-endoded string). ...
    (comp.lang.javascript)