请看下面两段代码
CString a = "abc";
a += '/0';
CString b = a;
CString a = "abc";
CString b = a + '\0';请问这两段代码有区别吗?为什么我用的时候(以b为一个函数的参数),第一段代码正确运行,第二段代码就总是运行时报错说无法读源文件,在网上查的时候有人说看一下CString是如何重载+就知道原因了,但我没找着那段代码,请大家帮忙,谢谢。
CString a = "abc";
a += '/0';
CString b = a;
CString a = "abc";
CString b = a + '\0';请问这两段代码有区别吗?为什么我用的时候(以b为一个函数的参数),第一段代码正确运行,第二段代码就总是运行时报错说无法读源文件,在网上查的时候有人说看一下CString是如何重载+就知道原因了,但我没找着那段代码,请大家帮忙,谢谢。
CString a = "abc";
CString b = a ;
b += '/0';
friend CString operator +( const CString& string1, const CString& string2 );
throw( CMemoryException );friend CString operator +( const CString& string, TCHAR ch );
throw( CMemoryException );friend CString operator +( TCHAR ch, const CString& string );
throw( CMemoryException );friend CString operator +( const CString& string, LPCTSTR lpsz );
throw( CMemoryException );friend CString operator +( LPCTSTR lpsz, const CString& string );
throw( CMemoryException );Return ValueA CString object that is the temporary result of the concatenation. This return value makes it possible to combine several concatenations in the same expression.Parametersstring, string1, string2CString objects to concatenate.chA character to concatenate to a string or to concatenate a string to.lpszA pointer to a null-terminated character string.ResThe + concatenation operator joins two strings and returns a CString object. One of the two argument strings must be a CString object. The other can be a character pointer or a character. You should be aware that memory exceptions may occur whenever you use the concatenation operator since new storage may be allocated to hold temporary data. ExampleThe following example demonstrates the use of CString::operator +.// example for CString::operator +
CString s1( "abc" );
CString s2( "def" );
ASSERT( (s1 + s2 ) == "abcdef" );
CString s3;
s3 = CString( "abc" ) + "def" ; // Correct
s3 = "abc" + "def";
// Wrong! The first argument must be a CString.
int deleteFile(CString fileName){
...
}
CString a = "abc";
a += '/0';
CString b = a;
CString a = "abc";
CString b = a + '\0'
这两段代码都是加\0,效果是不一样的,第一段正确,第二段错误哦,我想知道为什么
虽然我们可以在string后面+'\0',但是这个字符串不能再进行任何其他字符串运算。任何运算都可能造成后续的哪个'\0'丢失!所以如果你想给SHFileOperation准备输入参数,建议你开辟一个TCHAR缓冲区,自己通过_tcscpy逐个拷贝,而不要指望用std::string或者CString通过+'\0'的方法加
CString a = "abc";
a += '/0';
CString b = a;
CString a = "abc";
CString b = a + '\0';
到底有什么区别呢?