sususususu12 2024-03-13 18:21 采纳率: 43.8%
浏览 4

于如下的MyString片段,请补全必要的函数,使得main函数通过编译且正确运行。

对于如下的MyString片段,请补全必要的函数,使得main函数通过编译且正确运行。

struct MyString{
private:
    //自定义了一个新的变量类型,这个类型表示一个字符串
    char *str = nullptr;//类内初始化
public:
    //构造函数
    MyString(const char *src) {
        printf("MyString(const char*)\n");
        if(src == nullptr)
            return;
        str = (char *) malloc (strlen(src) + 1);
        strcpy(str, src);
    }
    
    //默认的构造函数
    MyString() {
        printf("MyString()\n");
    };
    
    //拷贝构造函数 copy constructor
    MyString(const MyString& t) : str{nullptr} {
        printf("MyString(const MyString&)\n");
        //
        if(t.str == nullptr)
            return;
        str = (char *) malloc (strlen(t.str) + 1);
        strcpy(str, t.str);
    }
    
    //析构函数
    ~MyString(){
        printf("~MyString()\n");
        free (str);
    }
    
    //操作符=的重载
    MyString& operator =(const MyString& other) {
        if (this == &other)
            return *this;
        free (str);
        str = nullptr;
        
        if(other.str == nullptr)
            return *this;
        str = (char *) malloc( strlen(other.str) + 1);
        strcpy(str, other.str);
        return *this;
    }
 };
 
 int main() {
    MyString c = {"Dmm"};
    MyString a;
    MyString d{" is a good man."};
    a = a + c += d;
    printf("%s\n", a.c_str());
    a[0] = 'd';
    printf("%s\n", a.c_str());

    return 0;
}

程序输出

MyString(const char*)

MyString()

MyString(const char*)

Dmm is a good man.

dmm is a good man.

~MyString()

~MyString()

~MyString()
感谢

  • 写回答

3条回答 默认 最新

  • 专家-赤兔[在线] 全栈领域优质创作者 2024-03-13 18:21
    关注

    引自免费微信小程序:皆我百晓生

    为了使main函数能够正确编译和运行,你需要在MyString类中添加两个成员函数:重载的+运算符以实现字符串拼接,并实现一个成员函数c_str()返回指向字符串内容的常量指针。以下是补全后的代码:

    #include <cstring>
    
    struct MyString{
    private:
        char *str = nullptr;
    public:
        // 之前已有的构造函数、拷贝构造函数、析构函数和赋值运算符重载...
    
        // 添加重载的 + 运算符实现字符串拼接
        MyString operator+(const MyString& other) const {
            printf("MyString::operator+\n");
            size_t this_len = str ? strlen(str) : 0;
            size_t other_len = other.str ? strlen(other.str) : 0;
            char *new_str = (char *) malloc(this_len + other_len + 1);
            strcpy(new_str, str ? str : "");
            strcat(new_str, other.str ? other.str : "");
    
            MyString result(new_str);
            free(new_str);
            return result;
        }
    
        // 重载 += 运算符
        MyString& operator+=(const MyString& other) {
            printf("MyString::operator+=\n");
            if (other.str == nullptr)
                return *this;
    
            size_t this_len = str ? strlen(str) : 0;
            size_t other_len = strlen(other.str);
            str = (char *) realloc(str, this_len + other_len + 1);
            strcat(str, other.str);
    
            return *this;
        }
    
        // 添加 c_str() 函数,返回指向字符串内容的常量指针
        const char* c_str() const {
            return str ? str : "";
        }
    };
    
    int main() {
        MyString c = {"Dmm"};
        MyString a;
        MyString d{" is a good man."};
        a = a + c; // 使用新定义的 + 运算符
        a += d;    // 使用新定义的 += 运算符
        printf("%s\n", a.c_str()); // 使用新定义的 c_str() 函数
    
        a[0] = 'd';
        printf("%s\n", a.c_str());
    
        return 0;
    }
    

    现在,这段程序应该可以正确编译并产生预期输出:

    MyString(const char*)
    MyString()
    MyString(const char*)
    MyString::operator+
    MyString::operator+=
    Dmm is a good man.
    dmm is a good man.
    ~MyString()
    ~MyString()
    ~MyString()
    
    评论 编辑记录

报告相同问题?

问题事件

  • 创建了问题 3月13日

悬赏问题

  • ¥15 两台交换机分别是trunk接口和access接口为何无法通信,通信过程是如何?
  • ¥15 C语言使用vscode编码错误
  • ¥15 用KSV5转成本时,如何不生成那笔中间凭证
  • ¥20 ensp怎么配置让PC1和PC2通讯上
  • ¥50 有没有适合匹配类似图中的运动规律的图像处理算法
  • ¥15 dnat基础问题,本机发出,别人返回的包,不能命中
  • ¥15 请各位帮我看看是哪里出了问题
  • ¥15 vs2019的js智能提示
  • ¥15 关于#开发语言#的问题:FDTD建模问题图中代码没有报错,但是模型却变透明了
  • ¥15 uniapp的h5项目写一个抽奖动画