简体   繁体   中英

initializer_list assignment overloading

#include <cassert>
#include <iostream>

using namespace std;
#include <initializer_list>

class IntArray {
    unsigned mLength = 0;
    int *mData = nullptr;

public:
    IntArray(unsigned length) : mLength(length) { mData = new int[length]; }
    ~IntArray() { delete[] this->mData; }
    IntArray(const std::initializer_list<int> &list) : IntArray(list.size()) {
        int count = 0;
        for (auto &e : list) {
            mData[count] = e;
            ++count;
        }
    }

    friend ostream &operator<<(ostream &os, IntArray &arr) {
        for (unsigned i = 0; i < arr.mLength; ++i) {
            os << arr.mData[i] << " ";
        }
        os << endl;
        return os;
    }

    //=operator
    IntArray &operator=(const std::initializer_list<int> &array) {
        delete[] mData;
        mLength = array.size();
        if (mData != nullptr) {
            // mData = new int[mLength+1];
            cout << "mLength is " << mLength << endl;
            mData = new int[mLength];
            int i{};
            for (auto &e : array) {
                mData[i++] = e;
                // There's a buffer overrun due to this line
                // so i change
                // mData = new int[mLength]; to mData = new int[mLength+1];
                // Is this bad?
                // Let me know if there's another better way.
            }
        } else {
            mData = nullptr;
        }
        return *this;
    }
};

int main() {
    IntArray intArray = {11, 12, 13, 14, 15, 16, 17, 18, 19, 20};
    intArray = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    cout << intArray << endl;
}

buffer overrun problem keep bodering me.. coding masters mData[i++] = e; There's a buffer overrun due to this line It's more than the index of the array, right? so i change this linemData = new int[mLength]; to mData = new int[mLength+1]; Is this bad? Let me know if there's another better way. plzplz coding is so hard... r u guys are all genius??

Your code runs perfectly on clang, gcc and msvc. There is no issue with what you're referring to whatsoever, however, I want to point out to something in the IntArray &operator=(const std::initializer_list<int> &array); , the first line of this method is delete[] mData; which may cause some undefined behavior if mData was nullptr , so change it to if(mData) delete[] mData; , the else{} statement doesn't run so I'd suggest you change change it to something like this

IntArray &operator=(const std::initializer_list<int> &array) {
    if(mData) delete[] mData;
    mLength = array.size();
    cout << "mLength is " << mLength << endl;
    mData = new int[mLength];
    int i{};
    for (auto &e : array) {
        mData[i++] = e;
    }
    return *this;
}

This is a possible implementation of it

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM