sususususu12 2024-03-13 17:38 采纳率: 43.8%
浏览 3

C++问题对于如下的MyString片段,使得main函数通过编译且正确运行。

C++问题


#include <iostream>
#include <cstring>

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");
    };

    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;
    }

    MyString operator+(const MyString& other) {
        MyString temp;
        if (str == nullptr && other.str == nullptr)
            return temp;
        else if (str == nullptr)
            temp = other;
        else if (other.str == nullptr)
            temp = *this;
        else {
            temp.str = (char*)malloc(strlen(str) + strlen(other.str) + 1);
            strcpy(temp.str, str);
            strcat(temp.str, other.str);
        }
        return temp;
    }

    MyString& operator+=(const MyString& other) {
        char* temp = nullptr;
        if (str == nullptr && other.str == nullptr)
            return *this;
        else if (str == nullptr)
            temp = strdup(other.str);
        else if (other.str == nullptr)
            return *this;
        else {
            temp = (char*)malloc(strlen(str) + strlen(other.str) + 1);
            strcpy(temp, str);
            strcat(temp, other.str);
        }
        free(str);
        str = temp;
        return *this;
    }

    const char* c_str() const {
        return str;
    }

    char& operator[](size_t index) {
        return str[index];
    }
};

int main() {
    MyString c = {"Dmm"};
    MyString a;
    MyString d{" is a good man."};
    a += c;
    a += d;
    a = a + c;
    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条回答 默认 最新

  • 关注

    sususususu12 下午好🌅🌅🌅
    本答案参考ChatGPT-3.5

    要输出 "MyString(const char*) MyString() MyString(const char*) Dmm is a good man. dmm is a good man. ~MyString() ~MyString() ~MyString()", 需要对代码进行一些修改:

    1. 将"printf"函数替换为"std::cout",并包含头文件,以使用std::setw()函数格式化输出。
    2. 为了能够正确调用构造函数MyString(const char*),需要为MyString类添加一个显式的默认构造函数。
    3. 修改构造函数MyString(const MyString&),以避免复制空指针。
    4. 修改operator+=()函数,以确保正确的内存分配。
    5. 修改operator函数,以处理空指针的情况。

    以下是修改后的代码:

    #include <iostream>
    #include <cstring>
    #include <iomanip>
    
    struct MyString {
    private:
        char* str = nullptr;
    
    public:
        MyString(const char* src) {
            std::cout << "MyString(const char*)" << std::endl;
            if (src == nullptr)
                return;
            str = new char[strlen(src) + 1];
            strcpy(str, src);
        }
    
        MyString() {
            std::cout << "MyString()" << std::endl;
            str = new char[1];
            *str = '\0';
        };
    
        MyString(const MyString& t) : str{ nullptr } {
            std::cout << "MyString(const MyString&)" << std::endl;
            if (t.str == nullptr)
                return;
            str = new char[strlen(t.str) + 1];
            strcpy(str, t.str);
        }
    
        ~MyString() {
            std::cout << "~MyString()" << std::endl;
            delete[] str;
        }
    
        MyString& operator=(const MyString& other) {
            if (this == &other)
                return *this;
            delete[] str;
            str = nullptr;
    
            if (other.str == nullptr)
                return *this;
            str = new char[strlen(other.str) + 1];
            strcpy(str, other.str);
            return *this;
        }
    
        MyString operator+(const MyString& other) {
            MyString temp;
            if (str == nullptr && other.str == nullptr)
                return temp;
            else if (str == nullptr)
                temp = other;
            else if (other.str == nullptr)
                temp = *this;
            else {
                temp.str = new char[strlen(str) + strlen(other.str) + 1];
                strcpy(temp.str, str);
                strcat(temp.str, other.str);
            }
            return temp;
        }
    
        MyString& operator+=(const MyString& other) {
            char* temp = nullptr;
            if (str == nullptr && other.str == nullptr)
                return *this;
            else if (str == nullptr)
                temp = strdup(other.str);
            else if (other.str == nullptr)
                return *this;
            else {
                temp = new char[strlen(str) + strlen(other.str) + 1];
                strcpy(temp, str);
                strcat(temp, other.str);
            }
            delete[] str;
            str = temp;
            return *this;
        }
    
        const char* c_str() const {
            return str;
        }
    
        char& operator[](size_t index) {
            return str[index];
        }
    };
    
    int main() {
        MyString c = "Dmm";
        MyString a;
        MyString d = " is a good man.";
        a += c;
        a += d;
        a = a + c;
        std::cout << std::setw(20) << std::left << a.c_str() << std::endl;
        a[0] = 'd';
        std::cout << std::setw(20) << std::left << a.c_str() << std::endl;
    
        return 0;
    }
    

    希望能对你有所帮助!

    评论

报告相同问题?

问题事件

  • 创建了问题 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项目写一个抽奖动画