gprentice Messages: 260 Registered: November 2005 Location: New Zealand
Experienced Member
From looking at String source code, it stores a null at the end of a string and has an operator funciton returning const char * so you can actually just use your string object anywhere a const char * is needed - or you could explicitly cast it.
(Maybe Mirek can confirm the null termination is always there ...)
void gp1(const char * p)
{
PromptOK(p);
}
void HelloWorld::About()
{
PromptOK("{{1@5 [@9= This is the]::@2 [A5@0 Ultimate`+`+ Hello world sample}}");
Yes, you can always cast to "const char *" and be always sure there is terminating 0.
BTW, this cast is implicit, so as long as there are no ambiguities, you can just write
String q;
....
const char *s = q;
operator~ is there to solve ambiguities usually resulting from overloading.
Note that U++ string does not support non-const char * access - this is because such thing would break COW implementation strategy. If you need to change individual characters of String, you can use Set method or StringBuffer. Note also that using StringBuffer instead of String is slightly faster in situations where you "building" String out of individual characters (and it then can be easily converted to String in low-cost operation).