Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » U++ Library support » U++ Core » How do I get a char * of the string stored in the String class?
How do I get a char * of the string stored in the String class? [message #97] Thu, 24 November 2005 10:29 Go to next message
jeff1101 is currently offline  jeff1101
Messages: 11
Registered: November 2005
Location: Philippines
Promising Member

How do I get a char * of the string stored in the String class?

The String class doesnt have a method like .c_str() or .chars()
Re: How do I get a char * of the string stored in the String class? [message #102 is a reply to message #97] Thu, 24 November 2005 12:01 Go to previous messageGo to next message
gprentice is currently offline  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}}");

String abc = "hello";
gp1(abc);
}

Graeme
Re: How do I get a char * of the string stored in the String class? [message #103 is a reply to message #97] Thu, 24 November 2005 12:09 Go to previous messageGo to next message
gprentice is currently offline  gprentice
Messages: 260
Registered: November 2005
Location: New Zealand
Experienced Member


And I just noticed the squiggle operator returns a const char * so you don't need to cast get a const char *.


void gp1(const char * p)
{
	PromptOK(p);
}


void HelloWorld::About()
{
	PromptOK("{{1@5 [@9= This is the]::@2 [A5@0 Ultimate`+`+ Hello world sample}}");

	String abc = "hello";
	gp1(~abc);
}


Graeme
Re: How do I get a char * of the string stored in the String class? [message #105 is a reply to message #102] Thu, 24 November 2005 13:35 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
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).
Re: How do I get a char * of the string stored in the String class? [message #113 is a reply to message #97] Fri, 25 November 2005 04:05 Go to previous message
jeff1101 is currently offline  jeff1101
Messages: 11
Registered: November 2005
Location: Philippines
Promising Member

Wow, thanks a lot. Razz
Next Topic: Dynamic Linking
Goto Forum:
  


Current Time: Mon Apr 29 14:33:19 CEST 2024

Total time taken to generate the page: 0.03784 seconds