Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Awefully Noobish - C++ Strings

Status
Not open for further replies.

wkc073406

Technical User
Nov 14, 2005
1
US
It's not very often that I need help with technical issues, but this is definitely one of those situations.

I'm a VB type that has just spent his first couple of hours with C++. Unbelievable, I'm having the steepest learning curve with Strings. Unfortunately, I'm not just jacking around, but have a project that requires me to develop a C++ DLL.

Could someone offer some insight on the declaration and use of strings? Even what you can and cannot do when passing various strings to function initiators? I know the very basics of C vs. C++ strings and narrow and wide strings. Every C++ string tutorial I've looked at neglects to mention the various permutations of syntax, e.g. '*' (wchar_t*)wstrvariable and so on.

Here's some code that passes wstrings around to functions. Of course, the compiler is mad at me :(. I've tried to model my actions around existing code I've reviewed, but I'm still missing the big picture.

Function A - queryregforaffirm is being called and I'm getting an error on the second Function B initiator. There are other string related issues, but maybe this will serve as a good test case to get me on the right track.
...
iRegQuery = queryregforaffirm(RegRootKey, L"TGroup" + i + L"AlphaLReq");
...

Function B
LONG queryregforaffirm(RegRootKey, wchar_t* wstrRegName)

Thanks in advance!

Ned

 
Code:
L"TGroup" + i + L"AlphaLReq"
I don't use L, I always use _T(), so I'm not sure what the rules are about using L, but I know you can't concatenate char* or literal strings like that.
Since char isn't an object, there is no + operator defined for it, so when you say:
"abc" + "xyz"
The compiler scratches its head and says, I don't know what the hell you're trying to do...

On the other hand, the STL string class (or wstring for wide strings) does understand the + operator. So you could do this:
Code:
string someString( "Hello" );
someString = someString + " World";
But you also need to convert i (which I'm assuming is an int) to a string, and string only understands strings, not numbers. So there's another class called stringstream (or wstringstream) which acts like cout, but instead of printing to the screen, it prints to its internal string buffer. Ex:
Code:
wstringstream regName;
regName << "TGroup" << i << "AlphaLReq";

iRegQuery = queryregforaffirm( RegRootKey, regName.str().c_str() );
Note the .str().c_str()
.str() is a function in stringstream that returns the contents of its buffer as an STL string object.
.c_str() is a function in string that returns a const char* version of its string.

The C way of doing all this is with the sprintf() function (or the wsprintf() function which I believe is a Microsoft specific function):
Code:
wchar_t someString[ 256 ];

sprintf( someString, "%s%d%s", "TGroup", i, "AlphaLReq" );
However, you should avoid the C style functions like printf(), sprintf()... because they can blow up easier and can have buffer overflow issues.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top