在前面博客中已经介绍过了普通的深拷贝构造函数和普通的赋值运算符重载函数,这里就不多做介绍了。
一.继普通拷贝构造函数之上的简单拷贝构造和简单赋值运算符重载函数
代码如下:
class String
{
private:
char *arr;
public:
String(const char *p = "")
{
if (NULL == p)
{
return;
}
this->arr = new char[strlen(p) + 1];//开辟空间
strcpy(this->arr, p);
cout << "执行构造函数" << endl;
}
~String()
{
delete[]arr;
arr = NULL;
cout << "执行析构函数" << endl;
}
String(const String &s):arr(NULL)//简化版拷贝构造函数,
{
String temp(s.arr);//执行构造函数~
swap(arr, temp.arr);
}//退出函数时析构对象temp
String& operator=( String s)//执行这个等号运算符重载前必须执行拷贝构造函数,构造匿名对象~
{
swap(arr, s.arr);
return *this;
}
/*String & operator=(const String &s)
{
String temp(s.arr);
swap(arr, temp.arr);
return *this;
}*/
friend ostream& operator<<(ostream &out, const String &n)//左移运算符重载
{
out << n.arr;
return out;
}
};
自行测试。
接下来重点介绍写时拷贝构造函数
思路图如下
代码如下:外加了几个运算符重载
class String
{
private:
char* _pStr;
public:
String(char* pStr = "")
{
_pStr = new char[strlen(pStr) + 1 + 4];
*(int *)_pStr = 1;
strcpy(_pStr + 4, pStr);
}
String(const String& s)
{
this->_pStr = s._pStr;
(*(int *)(_pStr))++;
}
String& operator=(String& s)
{
if (--(*(int *)(_pStr)) == 0)
{
delete[] _pStr;
_pStr = NULL;
}
this->_pStr = s._pStr;
(*(int *)(_pStr))++;
return *this;
}
~String()
{
if (0 == --(*(int *)(_pStr)))
{
delete[] _pStr;
_pStr = NULL;
}
}
char& operator[](size_t index)
{
--(*(int *)(_pStr));
char *p = new char[strlen(_pStr + 4) + 1 + 4];//开辟一块一样大的空间~
cout << strlen(_pStr) + 1 + 8;
(*(int *)(p)) = 1;
strcpy(p + 4, _pStr + 4);
_pStr = p;
return this->_pStr[index + 4];
}
friend ostream& operator<<(ostream &out, const String &n)
{
out << n._pStr + 4;//指向的是开辟内存的起始位置,所以要加4
return out;
}
bool operator>(String& s)
{
if (_pStr == s._pStr)
return false;
while (*(_pStr+4) != '\0'&&*(s._pStr+4) != '\0')
{
while((*(_pStr+4)==*(s._pStr+4)))
{
_pStr++;
s._pStr++;
if (*(_pStr + 4) == '\0'&&*(s._pStr + 4) == '\0')
break;
}
if (*(_pStr + 4) > *(s._pStr + 4))
return true;
else
return false;
}
}
bool operator<(String& s)
{
if (_pStr == s._pStr)
return false;
while (*(_pStr + 4) != '\0'&&*(s._pStr + 4) != '\0')
{
while ((*(_pStr + 4) == *(s._pStr + 4)))
{
_pStr++;
s._pStr++;
if (*(_pStr + 4) == '\0'&&*(s._pStr + 4) == '\0')
break;
}
if (*(_pStr + 4) < *(s._pStr + 4))
return true;
else
return false;
}
}
bool operator==(String& s)
{
if (_pStr == s._pStr)
return false;
while (*(_pStr + 4) != '\0'&&*(s._pStr + 4) != '\0')
{
while ((*(_pStr + 4) == *(s._pStr + 4)))
{
_pStr++;
s._pStr++;
if (*(_pStr + 4) == '\0'&&*(s._pStr + 4) == '\0')
return true;
}
return false;
}
}
bool operator!=( String& s)
{
if (_pStr == s._pStr)
return false;
while (*(_pStr + 4) != '\0'&&*(s._pStr + 4) != '\0')
{
while ((*(_pStr + 4) == *(s._pStr + 4)))
{
_pStr++;
s._pStr++;
if (*(_pStr + 4) == '\0'&&*(s._pStr + 4) == '\0')
return false;
}
return true;
}
}
bool strstr(const String& s)
{
if (_pStr == s._pStr)
return true;
char *dest=NULL;
char *src = NULL;
while (*(_pStr + 4) != '\0')
{
dest = _pStr + 4;
src = s._pStr + 4;
while (*dest++ == *src++)
{
if (*src == '\0')
{
return true;
}
}
_pStr++;
}
return false;
}
};