C++ 一个自定义的 class String 赋值给一个 C 风格的 String
C++ A self-defined class String assigning it to a C-style String
在我的作业中,我创建了一个名为 String 的 class。这是模板:
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' 是我在上面定义的 class 字符串(一个对象),而 "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;
}
谁能告诉我应该如何编辑赋值运算符函数以使其工作?
你的 class 已经有一个接受 String
作为输入的 operator=
和一个接受 const char*
作为输入的非 explicit
构造函数.因此,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()
等)也在正确管理分配的内存。
在我的作业中,我创建了一个名为 String 的 class。这是模板:
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' 是我在上面定义的 class 字符串(一个对象),而 "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;
}
谁能告诉我应该如何编辑赋值运算符函数以使其工作?
你的 class 已经有一个接受 String
作为输入的 operator=
和一个接受 const char*
作为输入的非 explicit
构造函数.因此,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()
等)也在正确管理分配的内存。