在我的作业中,我创建了一个名为 String 的类。这是模板:
class String
{
// the raw string buffer
char *m_pString;
// the capacity of this buffer
int m_capacity; // note: this is not the length of the string!
public:
// the default constructor
String(int size = 1);
// a constructor from a char-string
String(const char* src, int size = 1);
// a copy constructor
String(const String& src, int size = 1);
// the assignment operator
String& operator=(const String& src);
// the destructor
~String();
// a method to resize the buffer
virtual void resize(int newsize);
// method to return the current capacity of the buffer
int capacity() { return m_capacity; }
// method to return the length of the string
int length() const;
// true if the string is empty
bool empty() const;
// return a substring of the string
String substr(int index, int len)const;
// operators functions
// add a char to the end of the string
String& operator+= (char c);
// append a string to the end of this one
String& operator+= (const String& s2);
// return string A + string B
String operator+ (const String& s2) const;
// cast the string as a C-style string
operator const char* () const;
// true if the string is valid, false if empty
operator bool() const;
// true if string 1 == string 2
bool operator==(const String& s2)const;
};
我的问题是如果我们这样写会发生什么:
String a;
a = "hello";
“a”属于我在上面定义的类 String(一个对象),而“hello”是一个 c 风格的字符串,我不明白它是如何工作的,因为它们在我的主函数中任务,我正在努力让它发挥作用。
这是赋值运算符的定义,我认为这是我的问题所在:
String& String::operator=(const String& other)
{
int a = strlen(other.m_pString);
m_pString = nullptr;
m_pString = new char[a];
strcpy(m_pString, other.m_pString);
m_capacity = a;
return *this;
}
谁能告诉我应该如何编辑赋值运算符函数以使其工作?
最佳答案
你的类已经有一个 operator=
接受一个 String
作为输入,一个非 explicit
构造函数接受一个 const char*
作为输入。因此,a = "hello"
将使用临时对象调用隐式转换,类似于:
String a;
a.operator=(String("hello"));
真正的问题是您的 operator=
正在泄漏内存,并且没有正确分配新内存。您在重新分配之前没有释放 m_pString
,也没有为空终止符分配足够的内存,strlen()
和 strcpy()
需要。
String& String::operator=(const String& other) {
int a = strlen(other.m_pString);
m_pString = nullptr; // <-- leak here!
m_pString = new char[a]; // <-- 'a' is too small!
strcpy(m_pString, other.m_pString);
m_capacity = a;
return *this;
}
你需要做一些更像这样的事情:
String& String::operator=(const String& other)
{
if (&other != this)
{
int a = strlen(other.m_pString) + 1;
delete[] m_pString;
m_pString = new char[a];
strcpy(m_pString, other.m_pString);
m_capacity = a;
}
return *this;
}
或者这个,哪个更安全:
String& String::operator=(const String& other)
{
if (&other != this)
{
String temp(other);
std::swap(m_pString, temp.m_pString);
std::swap(m_capacity, temp.m_capacity);
}
return *this;
}
当然,这些假设您的其他方法(构造函数、析构函数、substr()
等)也在正确管理分配的内存。
关于C++自定义类String赋值给C风格的String,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45628344/