这段代码的作用class CString { public: CString(const char *str) :_str(new char[strlen(str) + 5]) { _str += 4; strcpy(_str, str); GetRefCount() = 1; } CString(const CString&s) :_str(s._str) { ++GetRefCount(); } ~CString() { GetRefCount() -= 1; if (0 == GetRefCount()) { delete[] (_str - 4); } } int &GetRefCount() { return *((int *)(_str - 4)); } CString &operator=(const CString &s) { if (_str != s._str) { GetRefCount() -= 1; if (0 == GetRefCount()) { delete[] (_str - 4); } _str = s._str; GetRefCount()++; } return *this; } friend ostream &operator<<(ostream &out, const CString &s) { out << s._str; return out; } private: char *_str; };
时间: 2023-05-27 20:06:18 浏览: 61
这段代码定义了一个CString类,它用来表示一个字符串,并提供了两个构造函数。第一个构造函数接受一个const char*类型的参数,用来初始化一个CString对象,它会在堆上分配一块内存,大小为输入参数的长度+5(为了存储引用计数),并将输入参数的内容复制到这块内存中。同时,引用计数被初始化为1。
第二个构造函数是复制构造函数,它接受一个const CString&类型的参数,用来初始化一个新的CString对象,它会将输入参数的引用计数加1,并指向输入参数所指向的内存空间。
这个类还提供了一个析构函数,它会将引用计数减1,如果引用计数为0,则释放分配的内存空间。同时,它还提供了一个拷贝赋值运算符和一个移动赋值运算符,用来实现复制和移动操作。
相关问题
在MyString类的基础上完成一下功能: 重载运算符:+、+=、=、[]、>、<、>=、<=、== class MyString{ private: char *m_data; public: MyString(const char *str = 0); MyString(const MyString& rhs); ~MyString(); };C++,完整代码加中文注释
以下是基于MyString类完成的代码,包括了重载运算符:+、+=、=、[]、>、<、>=、<=、==:
```c++
#include <iostream>
#include <cstring>
using namespace std;
class MyString{
private:
char *m_data;
public:
MyString(const char *str = 0); // 构造函数
MyString(const MyString& rhs); // 拷贝构造函数
~MyString(); // 析构函数
MyString& operator=(const MyString& rhs); // 重载=运算符
MyString operator+(const MyString& rhs); // 重载+运算符
MyString& operator+=(const MyString& rhs); // 重载+=运算符
char& operator[](const int index); // 重载[]运算符
friend bool operator>(const MyString& lhs, const MyString& rhs); // 重载>运算符
friend bool operator<(const MyString& lhs, const MyString& rhs); // 重载<运算符
friend bool operator>=(const MyString& lhs, const MyString& rhs); // 重载>=运算符
friend bool operator<=(const MyString& lhs, const MyString& rhs); // 重载<=运算符
friend bool operator==(const MyString& lhs, const MyString& rhs); // 重载==运算符
friend ostream& operator<<(ostream& os, const MyString& str); // 重载<<运算符
};
// 构造函数
MyString::MyString(const char *str){
if (str == NULL){
m_data = new char[1];
*m_data = '\0';
} else {
int length = strlen(str);
m_data = new char[length + 1];
strcpy(m_data, str);
}
}
// 拷贝构造函数
MyString::MyString(const MyString& rhs){
int length = strlen(rhs.m_data);
m_data = new char[length + 1];
strcpy(m_data, rhs.m_data);
}
// 析构函数
MyString::~MyString(){
delete[] m_data;
}
// 重载=运算符
MyString& MyString::operator=(const MyString& rhs){
if (this == &rhs) return *this;
delete[] m_data;
int length = strlen(rhs.m_data);
m_data = new char[length + 1];
strcpy(m_data, rhs.m_data);
return *this;
}
// 重载+运算符
MyString MyString::operator+(const MyString& rhs){
MyString newString;
int length = strlen(m_data) + strlen(rhs.m_data);
newString.m_data = new char[length + 1];
strcpy(newString.m_data, m_data);
strcat(newString.m_data, rhs.m_data);
return newString;
}
// 重载+=运算符
MyString& MyString::operator+=(const MyString& rhs){
int length = strlen(m_data) + strlen(rhs.m_data);
char *temp = new char[length + 1];
strcpy(temp, m_data);
strcat(temp, rhs.m_data);
delete[] m_data;
m_data = temp;
return *this;
}
// 重载[]运算符
char& MyString::operator[](const int index){
return m_data[index];
}
// 重载>运算符
bool operator>(const MyString& lhs, const MyString& rhs){
return strcmp(lhs.m_data, rhs.m_data) > 0;
}
// 重载<运算符
bool operator<(const MyString& lhs, const MyString& rhs){
return strcmp(lhs.m_data, rhs.m_data) < 0;
}
// 重载>=运算符
bool operator>=(const MyString& lhs, const MyString& rhs){
return strcmp(lhs.m_data, rhs.m_data) >= 0;
}
// 重载<=运算符
bool operator<=(const MyString& lhs, const MyString& rhs){
return strcmp(lhs.m_data, rhs.m_data) <= 0;
}
// 重载==运算符
bool operator==(const MyString& lhs, const MyString& rhs){
return strcmp(lhs.m_data, rhs.m_data) == 0;
}
// 重载<<运算符
ostream& operator<<(ostream& os, const MyString& str){
os << str.m_data;
return os;
}
int main(){
MyString str1("Hello");
MyString str2("World");
MyString str3 = str1 + str2;
cout << str3 << endl;
str3 += str1;
cout << str3 << endl;
cout << str3[2] << endl;
cout << (str1 > str2) << endl;
cout << (str1 < str2) << endl;
cout << (str1 >= str2) << endl;
cout << (str1 <= str2) << endl;
cout << (str1 == str2) << endl;
MyString str4 = "Hello";
cout << str4 << endl;
return 0;
}
```
以上代码中,重载运算符的实现方式与普通函数是不同的。对于成员函数的运算符重载,需要在函数名前加上`operator`关键字,例如重载+运算符的函数名为`operator+`。对于非成员函数的运算符重载,需要在参数前加上对应的类名,例如重载>运算符的函数名为`operator>(const MyString& lhs, const MyString& rhs)`。
完成下列的 String 类,并在主函数 main( )中测试它。 class String { public: String(const char *str = NULL); // constructor String(const String &other); // copy constructor ~ String(void); // destructor String & operate =(char *str); String & operate =(const String &other);// 重载=运算符 int operator==(String &other); // 重载==运算符 int operator==(char *str); private: char *m_data; // used for storing the string int length; };
下面是 String 类的实现:
```cpp
#include <cstring>
class String {
public:
String(const char* str = NULL);
String(const String& other);
~String();
String& operator=(const char* str);
String& operator=(const String& other);
int operator==(String& other);
int operator==(const char* str);
private:
char* m_data;
int length;
};
String::String(const char* str) {
if (str == NULL) {
length = 0;
m_data = new char[1];
m_data[0] = '\0';
}
else {
length = strlen(str);
m_data = new char[length + 1];
strcpy(m_data, str);
}
}
String::String(const String& other) {
length = other.length;
m_data = new char[length + 1];
strcpy(m_data, other.m_data);
}
String::~String() {
delete[] m_data;
}
String& String::operator=(const char* str) {
if (m_data == str) {
return *this;
}
delete[] m_data;
if (str == NULL) {
length = 0;
m_data = new char[1];
m_data[0] = '\0';
}
else {
length = strlen(str);
m_data = new char[length + 1];
strcpy(m_data, str);
}
return *this;
}
String& String::operator=(const String& other) {
if (m_data == other.m_data) {
return *this;
}
delete[] m_data;
length = other.length;
m_data = new char[length + 1];
strcpy(m_data, other.m_data);
return *this;
}
int String::operator==(String& other) {
return strcmp(m_data, other.m_data) == 0;
}
int String::operator==(const char* str) {
return strcmp(m_data, str) == 0;
}
```
在主函数中可以这样测试:
```cpp
#include <iostream>
int main() {
String str1("hello");
String str2("world");
String str3(str1);
std::cout << "str1 = " << str1 << std::endl;
std::cout << "str2 = " << str2 << std::endl;
std::cout << "str3 = " << str3 << std::endl;
str2 = str1;
std::cout << "After str2 = str1, str2 = " << str2 << std::endl;
if (str1 == str2) {
std::cout << "str1 is equal to str2" << std::endl;
}
else {
std::cout << "str1 is not equal to str2" << std::endl;
}
if (str1 == "hello") {
std::cout << "str1 is equal to \"hello\"" << std::endl;
}
else {
std::cout << "str1 is not equal to \"hello\"" << std::endl;
}
return 0;
}
```