2

我想在 C++ 中连接 2 个字符串,我不能使用 char*。

我尝试了以下但不起作用:

#define url L"http://domain.com"
wstring s1 = url;
wstring s2 = L"/page.html";
wstring s = s1 + s2;
LPOLESTR o = OLESTR(s);

我需要一个 s1 和 s2 连接的字符串。有任何信息或网站可以对此进行更多解释吗?谢谢。

4

2 回答 2

4

OLESTR("s")L"s"与(and OLESTR(s)is )相同Ls,这显然不是您想要的。
用这个:

#define url L"http://domain.com"
wstring s1 = url;
wstring s2 = L"/page.html";
wstring s = s1 + s2;
LPCOLESTR o = s.c_str();

这给了你一个LPCOLESTR(即 a const LPOLESTR)。如果你真的需要它是非常量的,你可以将它复制到一个新的字符串中:

...
wstring s = s1 + s2;
LPOLESTR o = new wchar_t[s.length() + 1];
wcscpy(o, s.c_str()); //wide-string equivalent of strcpy is wcscpy
//Don't forget to delete o!

或者,为了完全避免使用 wstring(不推荐;将应用程序转换为在wstring任何地方使用 's比使用LPOLESTR's 更好):

#define url L"http://domain.com"
LPCOLESTR s1 = url;
LPCOLESTR s2 = L"/page.html";
LPOLESTR s = new wchar_t[wcslen(s1) + wcslen(s2) + 1];
wcscpy(s, s1); //wide-string equivalent of strcpy is wcscpy
wcscat(s, s2); //wide-string equivalent of strcat is wcscat
//Don't forget to delete s!
于 2010-05-26T15:32:32.777 回答
2

您缺少 L 来完成 s2 的作业。

wstring s2 = L"/page.html";
于 2010-05-26T15:12:38.500 回答