利用cstring构造String类和其操作
2014-09-28 13:53
232 查看
关键点:
(1)operator+操作完成,会调用String::String(const String & rhs)构造函数;
(2)引用返回问题:千万不要返回局部对象的引用。当函数执行完毕时,将释放分配给局部对象的存储空间。此时,对局部对象的引用就会指向不确定的内存。
返回非引用类型:函数的返回值用于初始化在跳用函数出创建的临时对象。用函数返回值初始化临时对象与用实参初始化形参的方法是一样 的。 如果返回类型不是引用,在调用函数的地方会将函数返回值复制给临时对象。且其返回值既可以是局部对象,也可以是求解表达式的结果。
返回引用:当函数返回引用类型时,没有复制返回值。相反,返回的是对象本身。
(1)operator+操作完成,会调用String::String(const String & rhs)构造函数;
(2)引用返回问题:千万不要返回局部对象的引用。当函数执行完毕时,将释放分配给局部对象的存储空间。此时,对局部对象的引用就会指向不确定的内存。
返回非引用类型:函数的返回值用于初始化在跳用函数出创建的临时对象。用函数返回值初始化临时对象与用实参初始化形参的方法是一样 的。 如果返回类型不是引用,在调用函数的地方会将函数返回值复制给临时对象。且其返回值既可以是局部对象,也可以是求解表达式的结果。
返回引用:当函数返回引用类型时,没有复制返回值。相反,返回的是对象本身。
#include <iostream> #include <cstring> using namespace std; class String { public: String(); String(const char *const); String(const String &); ~String(); char& operator[] (unsigned short offset); char operator[] (unsigned short offset)const; String operator+(const String&); void operator+=(const String&); String& operator= (const String &); unsigned short GetLen()const { return itsLen; } const char* GetString()const { return itsString;} private: String (unsigned short); char* itsString; unsigned short itsLen; }; String::String() { itsString = new char[1]; itsString[0] = '\0'; itsLen=0; cout<<"String() has been called! "<<endl; } String::String(unsigned short len) //这个干什么用? { itsString = new char[len+1]; for (unsigned short i =0;i<=len;i++) itsString[i] = '\0'; itsLen=len; cout<<"String::String(unsigned short len) has been called! "<<endl; } String::String(const char* const cString) //常量字符串调用初始化 { itsLen = strlen(cString); itsString = new char[itsLen+1]; for (unsigned short i=0;i<itsLen;i++) itsString[i] = cString[i]; itsString[itsLen] = '\0'; cout<<"String::String(const char* const cString) has been called! "<<endl; } String::String(const String & rhs) // { itsLen = rhs.GetLen(); itsString = new char[itsLen+1]; for (unsigned short i = 0;i<itsLen;i++) itsString[i] = rhs[i]; itsString[itsLen] = '\0'; cout<<"String::String(const String & rhs) has been called! "<<endl; cout<<"output is "<< itsString<<endl; } String::~String() { delete [] itsString; itsLen = 0; } String& String::operator =(const String& rhs) { if (this == &rhs) return *this; //自身赋值 delete [] itsString; itsLen=rhs.GetLen(); itsString = new char[itsLen+1]; for (unsigned short i = 0;i < itsLen;i++) itsString[i] = rhs[i]; itsString[itsLen] = '\0'; cout<<" operater = has been called! "<<endl; return *this; } // char& String::operator [](unsigned short offset) // char String::operator [](unsigned short offset)const 区别? char& String::operator [](unsigned short offset) { if (offset > itsLen) return itsString[itsLen-1]; else return itsString[offset]; } char String::operator [](unsigned short offset)const { if (offset > itsLen) return itsString[itsLen-1]; else return itsString[offset]; } String String::operator +(const String& rhs) { unsigned short totalLen = itsLen + rhs.GetLen(); String temp(totalLen); unsigned short i; for (i=0;i<itsLen;i++) temp[i] = itsString[i]; for (unsigned short j = 0;j<rhs.GetLen();j++,i++) temp[i] = rhs[j]; temp[totalLen] = '\0'; cout<<"String String::operator +(const String& rhs) has been called! "<<endl; return temp; //返回结果后,调用String::String(const String & rhs) } void String::operator +=(const String& rhs) { unsigned short rhsLen = rhs.GetLen(); unsigned short totalLen = itsLen + rhsLen; String temp(totalLen); unsigned short i; for (i = 0;i<itsLen;i++) temp[i] = itsString[i]; for (unsigned short j = 0;j<rhs.GetLen();j++,i++) temp[i] = rhs[i-itsLen]; temp[totalLen] = '\0'; *this = temp; } int main() { String s1("initial test"); //String s(s1); /* cout<<"S1:/t"<<s1.GetString()<<endl; char *temp ="Hello World"; s1 = temp; cout<<"S1:/t"<<s1.GetString()<<endl; char tempTwo[20]; strcpy(tempTwo,"; nice to be here!"); cout<<" s1:/t "<<s1.GetString()<<endl; s1 += tempTwo; cout<<"tempTwo:/t "<<tempTwo<<endl; cout<<"S1:/t "<<s1.GetString()<<endl; cout<<"S1 size is "<<s1.GetLen()<<endl; cout<<"S1[4]:/t "<<s1[4]<<endl; cout<<"S1[999]:/t "<<s1[999]<<endl; //越界没有问题?? */ String s2(" Anoter string"); //cout<<"s1 + s2 = "<<(s1+s2).GetString()<<endl; s1+s2; cout<<endl<<endl; String s3; s3 = s1+s2; //s1 + s2 后生成一个零时变量 cout<<"S3:/t " <<s3.GetString()<<endl; String s4; s4 = "Why does this work?"; cout<<"S4:/t "<<s4.GetString()<<endl; cout<<"'why are you ? '"<<"has "<<sizeof("why are you ?")<<" characters! "<<endl; cout<<"'why are you ? '"<<"has "<<strlen("why are you ?")<<" characters! "<<endl; return 0; }
相关文章推荐
- string类中,利用构造方法,实现实例化
- 黑马程序员--Java基础加强--16.利用反射操作泛型V【通过Constructor反射解析泛型构造方法】【通过Field反射解析泛型成员变量】【个人总结】
- 利用反射机制编写一个程序,这个程序能指定调用类的某个方法及构造方法,并把操作信息输出到控制台。
- SQL Server 2005: 利用新的ranking函数实现高效的数据分页操作
- 简单string类及对象构造测试
- 如何利用.net操作oracle里面的clob字段
- [转]利用VB操作XML数据
- 利用Oracle的Job Queue实现定时操作
- 在delphi里利用指针构造链表
- 利用数据库日志恢复数据到时间点的操作
- 利用C#对远程服务进行操作,如MSSQLSERVER等。
- 考虑大对象:利用 JAVA 操作 DB2 Universal Database 中的 LOB 数据
- 利用JAVA操作EXCEL文件 (转载)
- Relaxlife.net最强计数器-利用操作INI文件来控制流量,也可用做系统设置
- 利用堆栈方式对grid进行行排序的具体操作
- 利用GDI+函数构造图形报表
- 利用TDC组件实现对WEB页面的交互操作
- 利用Flashback Query 恢复误操作的数据ZZ
- 利用VB操作目录和文件夹
- 利用Session限制重复操作的简单方法