vendredi 28 décembre 2018

C++ value initialize items of a custom container

Lets take custom vector implementation as an example:

template<typename Object>
class myVector {
public:
    explicit myVector(int size = 0) :
        _size{ size },
        _capasity{ size + SPARE_CAPACITY }
    {
        _buff = new Object[_capasity];
        if (_size > 0) {
            for (int i = 0; i < _size; i++) {
                //_buff[i] = 0;
            }
        }
    }

// more code

private:
    Object * _buff = nullptr;
    int _size;
    int _capasity;
};

So my question is, how to make myVector be value-initialized in case I'll initialize it as:

int main() {
    myVector<int> v02(5);                   
}

Here, it contains 5 int values, so I need it to be all zeros; same with other types. I commented out _buff[i] = 0; as it's specific to int. Please give me some hints.

Aucun commentaire:

Enregistrer un commentaire