标签:
1 // 比如 2 // 3 // 这段实现了重载操作符 String + char* 的操作 4 5 const char* operator+(const char* str) { 6 int len = 0; 7 if( str ) 8 len = strlen(str); 9 if(pTmpString) 10 delete pTmpString; 11 12 int oldlen = strlen(pString); 13 pTmpString = new char[oldlen+len+1]; 14 pTmpString[oldlen+len] = 0; 15 wsprintfA(pTmpString,"%s%s",pString,str); 16 return pTmpString; 17 } 18
19 // 20 // 但是如果想实现 char* + String 的操作就必须使用友元函数了 21 // 22 // 因为他可以直接访问类里的私有数据 23 24 friend const char* operator+(const char* str,String &strclass) { 25 int len = 0; 26 if( str ) 27 len = strlen(str); 28 if(strclass.pTmpString) 29 delete strclass.pTmpString; 30 31 int oldlen = strlen(strclass.pString); 32 strclass.pTmpString = new char[oldlen+len+1]; 33 strclass.pTmpString[oldlen+len] = 0; 34 wsprintfA(strclass.pTmpString,"%s%s",str,strclass.pString); 35 return strclass.pTmpString; 36 }
标签:
原文地址:http://www.cnblogs.com/codetask/p/5602155.html