开发者

how to combine two LPCWSTRs?

I have two variables that are LPCWSTRs. I want to create a new variable that will have the values 开发者_JAVA技巧of the first and second variable.

I tried this but it didn't work.

LPCWSTR d = L"sd";
LPCWSTR f = L"f";
LPCWSTR df = d + f;

I get this error when i try that.

1   IntelliSense: expression must have integral or enum type

Is there a function that can combine two LPCWSTRs?


In C++ it is usually a good idea to use std::string for manipulations with strings. In your case it could look like:

LPCWSTR d = L"sd";
LPCWSTR f = L"f";
std::wstring df = std::wstring(d) + f;
LPCWSTR dfc = df.c_str(); // if you are really need this


It doesn't work because the C++ compiler cannot generate code to join together arrays. The two strings in the example are arrays of type wchar_t. To join arrays you must use higher level functions. There are several ways of doing it:

LPWSTR df[20]; // cannot be LPCWSTR, because the C is for const.
wcsprintf(df, L"%s%s", d, f);

or

LPWSTR df[20];
wcscpy(df, d);
wcscat(df, f);

or use STL as previously answered.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜