返回

c++如何在析构函数中使用delete[]

发布时间:2022-04-29 10:53:32 169
# golang

我有以下文件用于实现Bucket班然而,我不能在析构函数中销毁从operator+.另外,我如何访问_len属性中的属性operator>>函数以分配新值?我得到的错误是:

在正常阻止后检测到堆损坏....CRT检测到应用程序在堆缓冲区结束后写入内存

问题总是发生在这两个问题相加之后Bucket对象,但我在调试期间检查了生成的字符串以及长度是否正确。

头文件:

class Bucket
{
    friend ostream& operator<<(ostream& os, const Bucket& c);
    friend istream& operator>>(istream& is, const Bucket& c);
public:
    Bucket();
    Bucket(const String& str);
    Bucket(const char* str);
    ~Bucket();
    const Bucket operator+(const Bucket& s) const;
    const Bucket operator+(const char* s) const;
    const Bucket& operator=(const char* c);
    const bool operator==(const char* str);
private:
    char* _str;
    int _len;
}; 

源文件:

Bucket::Bucket() {
    _len = 0;
    _str = new char[_len];
}

Bucket::Bucket(const Bucket& myBucket) {
    _len = myBucket._len;
    _str = new char[_len + 1];
    for (int i = 0; i < _len; i++)
        _str[i] = myBucket._str[i];
    _str[_len] = '\0';
}

Bucket::Bucket(const char* str) {
    _len = 0;
    for (int i = 0; str[i] != '\0'; i++) _len++;
    
    _str = new char[_len + 1];
    for (int i = 0; i < _len; i++)
        _str[i] = str[i];
    _str[_len] = '\0';
}

Bucket::~Bucket() {
    if (_len > 0) delete[] _str;
}

const Bucket Bucket::operator+(const Bucket& myBucket) const {
    String tempBucket;
    tempBucket._str = strcat(this->_str, myBucket._str);
    int len = 0;
    
    while (tempBucket._str[len] != '\0') len++;
    tempBucket._str[len] = '\0';
    tempBucket._len = len;
    return tempBucket;
}

const Bucket Bucket::operator+(const char* str) const {
    Bucket tempBucket;
    
    int len = 0;

    tempBucket._len = this->_len;

    for (int i = 0; str[i] != '\0'; i++) tempBucket._len++;

    tempBucket._str = strcat(tempBucket._str, str);

    tempBucket._str[len] = '\0';
    tempBucket._len = len;
    return tempBucket;
}

const Bucket& Bucket::operator=(const char* str) {
    if (this->_str == str) {
        return *this;
    }
    
    _len = 0;
    for (int i = 0; str[i] != '\0'; i++) _len++;

    _str = new char[_len + 1];
    for (int i = 0; i < _len; i++)
        _str[i] = str[i];
    _str[_len] = '\0';

    return *this;
}

const bool Bucket::operator==(const char* str) {
    int comp = strcmp(this->_str, str);
    if (comp == 0) {
        return true;
    }
    else {
        return false;
    }
}

ostream& operator<<(ostream& os, const Bucket& myBucket) {
    os << myBucket._str;
    return os;
}

istream& operator>>(istream& is, const Bucket& myBucket) {
    static char buffer[40];
    is >> buffer;

    int len = 0;
    for (size_t i = 0; buffer[i] != '\0'; i++) {
        myBucket._str[i] = buffer[i];
        len++;
    }
    myBucket._str[len++] = '\0';

    return is;
}

主要内容:

int main()
{
    Bucket b1("Hello, "); // This is deleted by the destructor
    Bucket b2(b1); // This is deleted by the destructor
    cout << b1 << b2 << endl; 
    b2 = "Dear ";  // Also works fine
    Bucket b3;
    cout << "Enter a name: ";
    cin >> b3; // Can't assign the _len attribute
    cout << b2 + b3 << ",";  // not destroyed
    Bucket b4(" Please write this sentence after pressing enter:\n");
    b2 = "We believe that ";
    cout << b4 + b2 << b1 << "and " << "Goodbye "  // not destroyed
        << (b1 == "Goodbye " ? "is " : "is not ") << "the same word!\n" <<
        endl;
    return 0;
}
特别声明:以上内容(图片及文字)均为互联网收集或者用户上传发布,本站仅提供信息存储服务!如有侵权或有涉及法律问题请联系我们。
举报
评论区(0)
按点赞数排序
用户头像