对于我的 CS 课,我们的老师希望我们创建自己的字符串课。我正处于开始阶段,目前正在尝试重载 operator+
。他希望它作为非成员函数。这是我目前所拥有的:
这是 String
头文件:
#include <iostream>
#include <stdio.h>
#include <cstring>
#ifndef STRING_HPP
#define STRING_HPP
class String
{
private:
int len;
char* str;
public:
String(); // default constructor
String(String const& s); // copy constructor
String(char const* s); // C-string constructor
~String() {delete str;}; // destructor
char* const getString(); //get string for printing
String& operator=(char const* c);
String& operator=(String const& s);
};
inline std::ostream& operator<<(std::ostream& os, String s)
{
return os << s.getString();
}
String operator+(String const& lhs, String const& rhs) {
len = lhs.len + rhs.len;
str = new char[len];
std::strcat(str,lhs);
std::strcat(str,rhs);
}
#endif
这是 String.cpp
文件:
#include "string.hpp"
#include <cstring>
#include <iostream>
String::String()
{
len = 0;
str = new char[len];
}
String::String(String const& s) // copy constructor
{
len = s.len;
str = new char[len];
std::strcpy(str,s.str);
}
String::String(char const* s) // C-string constructor
{
len = std::strlen(s);
str = new char[len];
std::strcpy(str,s);
}
char* const String::getString()
{
return str;
}
String& String::operator=(char const* c)
{
// 1: allocate new memory and copy the elements
int newLen = std::strlen(c);
char* newStr = new char[newLen];
std::strcpy(newStr,c);
// 2: deallocate old memory
delete [] str;
// 3: assign the new memory to the object
str = newStr;
len = newLen;
}
String& String::operator=(String const& s)
{
// 1: allocate new memory and copy the elements
int newLen = s.len;
char* newStr = new char[newLen];
std::strcpy(newStr,s.str);
// 2: deallocate old memory
delete [] str;
// 3: assign the new memory to the object
str = newStr;
len = newLen;
}
这里是main
的实现:
#include "string.hpp"
#include <iostream>
int main()
{
String s1;
String s2 = "test";
String s3 = s2;
std::cout << s1 << '\n';
std::cout << s2 << '\n';
std::cout << s3 << '\n';
String a = "one";
String b = "two";
a = b;
b = "three";
std::cout << a << '\n';
std::cout << b << '\n';
String hello = "hello ";
String world = "world";
String concat = hello + world;
std::cout << concat << '\n';
}
一切正常,直到最后一个 concat
。我无法更改 Main.cpp
,因为它是由教授提供的。
谁能帮我指明正确的方向,让 operator+
正常工作?
最佳答案
您正在使用
str = new char[len];
在所有功能中。那没有足够的内存来保存终止的空字符。您需要使用:
str = new char[len+1];
在 operaor+
函数中,在内存分配之后使用 strcat
会导致未定义的行为,因为 str
未初始化。
你需要使用:
str = new char[len+1];
str[0] = '\0';
std::strcat(str,lhs);
std::strcat(str,rhs);
或
str = new char[len+1];
std::strpy(str,lhs); // strcpy, not strcat
std::strcat(str,rhs);
更新
operator+()
的完整实现:
String operator+(String const& lhs, String const& rhs) {
int len = lhs.len + rhs.len;
char* str = new char[len+1];
std::strcpy(str,lhs);
std::strcat(str,rhs);
String ret(str);
delete [] str;
return ret;
}
如果您实现String::operator+=(String const& rhs)
,您可以简化函数。然后,您可以使用:
String operator+(String const& lhs, String const& rhs) {
String ret(lhs);
ret += rhs;
return ret;
}
关于c++ - 运算符重载问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33269238/