- [NEWBIE] Display a string object by means of TextOut()
- Posted by Massimiliano on October 7th, 2003
I'm trying to display a std::string object by a TextOut(), but I get
this error:
"cannot convert 'std::string' to 'const CHAR *' for argument 4 to
'BOOL TextOut( ....)' Should I cast the string into a char *?
Thx!
- Posted by Allan Bruce on October 7th, 2003
"Massimiliano" <sirbone72@yahoo.it> wrote in message
news:51bddbbe.0310070429.34fee3fb@posting.google.c om...
No, instead look at
c_str()
e.g. if you have
std::string SomeString = "Some Text";
TextOut(hdc, xPos, yPos, SomeString.c_str(), strlen(SomeString.c_str()));
HTH
Allan
- Posted by Jerry Coffin on October 7th, 2003
In article <bluci7$1hg$1@news.freedom2surf.net>, allanmb@TAKEAWAYf2s.com
says...
[ ... ]
While this will work, it's needlessly inefficient. I'd use:
TextOut(hdc, xPos, yPos, SomeString.c_str(), SomeString.size());
Of course, if you're doing this very often at all, it's probably better
to write your own little adapter function:
inline BOOL
TextOut(HDC hdc, int xStart, int yStart, std::string const &s) {
return TextOut(hdc, xPos, yPos, s.c_str(), s.size());
}
And then you can ignore the messy details, and just pass an std::string
directly, something like this:
std::string x("This is a string");
TextOut(hdc, 0, 0, x);
As a bonus, this will also handle writing out string literals:
TextOut(hdc, 10, 10, "This is another string");
by constructing a temporary std::string from the literal, and then
invoking the function above to write that out.
--
Later,
Jerry.
The universe is a figment of its own imagination.
- Posted by Jerry Coffin on October 7th, 2003
In article <MPG.19ec79a6e548e26f989b5a@news.clspco.adelphia.n et>,
jcoffin@taeus.com says...
[ ... ]
Oops -- that should (obviously) be:
inline BOOL
TextOut(HDC hdc, int xPos, int yPos, std::string const &s) {
return TextOut(hdc, xPos, yPos, s.c_str(), s.size());
}
I.e. the parameter names have to match for things to work. My
apologies.
--
Later,
Jerry.
The universe is a figment of its own imagination.
- Posted by Allan Bruce on October 7th, 2003
"Jerry Coffin" <jcoffin@taeus.com> wrote in message
news:MPG.19ec79a6e548e26f989b5a@news.clspco.adelph ia.net...
How stupid of me! Of course this is much better than strlen(s.c_str())
- Posted by Massimiliano on October 8th, 2003
Thanks! It works perfectly! I better study string documentation . . .