OurString& OurString::operator=(const OurString& str) { if (this == &str) return *this; if(ptr != NULL) { delete [] ptr; } ptr = new char[str.len + 1]; assert(ptr != 0); ptr = strcpy(ptr, str.ptr); len = str.len; return *this; } 你可以把上述的参数改成const char *str吗
时间: 2024-03-23 22:35:57 浏览: 75
可以修改参数为`const char* str`,但是在代码中需要修改一些地方。由于现在的参数类型是指向字符数组的指针,我们不能像之前那样使用`str.len`,因为指针没有`len`属性。我们需要在代码中添加一行来计算字符串长度,例如使用`strlen(str)`函数来获取字符串的长度。修改后的代码如下:
```
OurString& OurString::operator=(const char* str) {
if (ptr != NULL) {
delete[] ptr;
}
len = strlen(str);
ptr = new char[len + 1];
assert(ptr != 0);
ptr = strcpy(ptr, str);
return *this;
}
```
需要注意的是,在这个函数中我们没有检查`str`是否为空指针。如果`str`是空指针,那么调用`strlen(str)`函数将会导致程序崩溃。因此,在实际使用中,我们需要在函数开头添加一行代码来检查`str`是否为空指针。
相关问题
填充下面这个程序中所有出现// TODO: fill the code这个任务的地方#include <iostream> #include <cstring> #include "ourstring.h" #include "strlib.h" using namespace std; OurString::OurString(){ // TODO: fill the code } OurString::OurString(const char *str){ // TODO: fill the code } OurString::OurString(const OurString &dstr){ // TODO: fill the code } OurString::~OurString(){ // TODO: fill the code } string OurString::toString() const{ // TODO: fill the code } OurString OurString::subStr(unsigned int start, unsigned int n) const{ // TODO: fill the code } bool OurString::operator > (const OurString &dstr) const{ // TODO: fill the code } bool OurString::operator < (const OurString &dstr) const{ // TODO: fill the code } bool OurString::operator == (const OurString &dstr) const{ // TODO: fill the code } unsigned int OurString::length () const{ // TODO: fill the code } const OurString& OurString::operator = (const OurString &dstr){ // TODO: fill the code } const OurString& OurString::operator = (const char *str){ // TODO: fill the code } char& OurString::operator[](int index){ // TODO: fill the code } const OurString OurString::operator + (const OurString &dstr) const{ // TODO: fill the code } const OurString OurString::operator + (const char *str) const{ // TODO: fill the code } const OurString& OurString::operator += (const OurString &dstr){ // TODO: fill the code } const OurString& OurString::operator += (const char *str){ // TODO: fill the code } ostream & operator<<(ostream &os, const OurString &dstr){ // TODO: fill the code } istream & operator>>(istream &is, OurString &dstr){ // TODO: fill the code }
好的,下面是填充后的代码,每个 TODO 已经被替换成相应的实现:
```c++
#include <iostream>
#include <cstring>
#include "ourstring.h"
#include "strlib.h"
using namespace std;
OurString::OurString(){
str = new char[1];
str[0] = '\0';
}
OurString::OurString(const char *str){
this->str = new char[strlen(str)+1];
strcpy(this->str, str);
}
OurString::OurString(const OurString &dstr){
this->str = new char[dstr.length()+1];
strcpy(this->str, dstr.str);
}
OurString::~OurString(){
delete [] str;
}
string OurString::toString() const{
return string(str);
}
OurString OurString::subStr(unsigned int start, unsigned int n) const{
if (start >= length()) return OurString();
n = (n > length() - start) ? length() - start : n;
return OurString(str + start, str + start + n);
}
bool OurString::operator > (const OurString &dstr) const{
return strcmp(str, dstr.str) > 0;
}
bool OurString::operator < (const OurString &dstr) const{
return strcmp(str, dstr.str) < 0;
}
bool OurString::operator == (const OurString &dstr) const{
return strcmp(str, dstr.str) == 0;
}
unsigned int OurString::length () const{
return strlen(str);
}
const OurString& OurString::operator = (const OurString &dstr){
if (this != &dstr) {
delete [] str;
str = new char[dstr.length()+1];
strcpy(str, dstr.str);
}
return *this;
}
const OurString& OurString::operator = (const char *str){
if (this->str != str) {
delete [] this->str;
this->str = new char[strlen(str)+1];
strcpy(this->str, str);
}
return *this;
}
char& OurString::operator[](int index){
return str[index];
}
const OurString OurString::operator + (const OurString &dstr) const{
OurString s;
s.str = new char[length() + dstr.length() + 1];
strcpy(s.str, str);
strcat(s.str, dstr.str);
return s;
}
const OurString OurString::operator + (const char *str) const{
OurString s;
s.str = new char[length() + strlen(str) + 1];
strcpy(s.str, this->str);
strcat(s.str, str);
return s;
}
const OurString& OurString::operator += (const OurString &dstr){
*this = *this + dstr;
return *this;
}
const OurString& OurString::operator += (const char *str){
*this = *this + str;
return *this;
}
ostream & operator<<(ostream &os, const OurString &dstr){
os << dstr.str;
return os;
}
istream & operator>>(istream &is, OurString &dstr){
string s;
is >> s;
dstr = s.c_str();
return is;
}
```
bool String::operator == (const char* s)const {return (strcmp(m_str,s ? s : "") == 0);}
这是一个字符串类中的重载运算符==,用于判断当前对象和传入的C风格字符串是否相等。具体实现是调用了C标准库函数strcmp(),比较当前对象的m_str成员和传入的s指针指向的字符串是否相等。如果相等返回0,否则返回非0值。需要注意的是,为了避免传入的s指针为NULL,代码中使用了三目运算符将其转为空字符串。
阅读全文