我尝试使用Microsoft Visual Studio 2015在C ++中编写自己的String类。我像这样编写了类;
#include<string.h>
class myString {
private:
char* content;
public:
int size;
myString();
myString(char*);
~myString();
bool operator== (const myString &) const;
bool operator!= (const myString &) const;
myString operator= (const myString &);
myString operator+ (const myString &) const;
myString operator+= (const myString &);
friend std::ostream& operator<< (std::ostream &os, const myString &);
char operator[] (int &) const;
};
std::ostream& operator<<(std::ostream &os, const myString &string) {
os << string.content;
return os;
}
myString::myString() {
size = 0;
content = "\0";
}
myString::myString(char* newContent) {
size = strlen(newContent);
content = new char[size+1];
strcpy(content, newContent);
}
myString::~myString() {
delete[] content;
}
myString myString::operator= (const myString &string) {
if (size != string.size) {
delete[] content;
size = string.size;
content = new char[size+1];
}
strcpy(content, string.content);
return *this;
}
bool myString::operator== (const myString &string) const {
if (size != string.size)
return false;
if (strcmp(content, string.content))
return false;
return true;
}
bool myString::operator!= (const myString &string) const {
if (*this == string)
return false;
return true;
}
myString myString::operator+ (const myString &string) const {
int newSize = size + string.size;
char* newContent = new char[newSize];
strcpy(newContent, content);
strcat(newContent, string.content);
return myString(newContent);
}
myString myString::operator+= (const myString &string) {
*this = *this + string;
return *this;
}
char myString::operator[] (int &index) const {
return content[index];
}
我尝试这样做时效果很好;
#include<iostream>
#include "MyString.h"
using namespace std;
int main() {
myString s("my new");
cout << s+" string" << endl;
}
但是我不确定行operator+
中的char* newContent = new char[newSize];
函数是否有任何内存泄漏我从内存中分配新空间我需要在return语句中return myString(newContent);
所以我不能在这行之前释放它,我不能在return语句之后释放它。我纠正了,是否有内存泄漏?如果是这样,我该如何解决这个问题?
编辑1:
我在Prince Dhaliwal的帮助下改变了operator+
功能如下;
myString myString::operator+ (const myString &string) const {
myString temp;
int newSize = size + string.size;
char* newContent = new char[newSize + 1];
temp.size = newSize;
strcpy(newContent, content);
strcat(newContent, string.content);
temp.content = newContent;
return temp;
}
但是因为我在本地创建了temp
,所以在返回之前调用它的析构函数并给出错误。我想我也应该为temp分配内存。我改变了函数如下;
myString myString::operator+ (const myString &string) const {
myString* temp= new myString;
int newSize = size + string.size;
char* newContent = new char[newSize+1];
temp->size = newSize;
strcpy(newContent, content);
strcat(newContent, string.content);
temp->content = newContent;
return *temp;
}
它现在运行正常,但我认为由于temp
变量仍然存在内存泄漏。如果有内存泄漏,如何解决这个问题?
编辑2: 我只是通过创建一个Copy Constructor来修复它
答案 0 :(得分:0)
您的代码中实际存在内存泄漏。当您在+
中使用s + " string"
运算符时。在你的operator+()
定义中,即
myString myString::operator+ (const myString &string) const {
int newSize = size + string.size;
char* newContent = new char[newSize];
strcpy(newContent, content);
strcat(newContent, string.content);
return myString(newContent);
}
您在此处分配新字符串char* newContent = new char[newSize];
,将旧部分和新部分复制到新字符串。您再次在构造函数return myString(newContent);
中分配新字符串。但是你在哪里删除你的旧字符串?它在你的代码中无处可去。所以你必须删除字符串newContent
。
你可以这样做
myString myString::operator+ (const myString &string) const {
myString temp;
int newSize = size + string.size;
char* newContent = new char[newSize + 1];
temp.size = newSize;
strcpy(newContent, content);
strcat(newContent, string.content);
temp.content = newContent;
return temp;
}
UPDATE 您必须创建一个复制构造函数。
myString(const myString &rhs) :
size(rhs.size) {
content = new char[size + 1];
strcpy(content, rhs.content);
}