dimanche 3 janvier 2021

Does this class need const versions of push_back and pop_back?

Does this class need const versions of push_back and pop_back?

class StrBlob {
    public:
        typedef std::vector<std::string>::size_type size_type;
        StrBlob();
        ~StrBlob();
        StrBlob(std::initializer_list<std::string> il);
        // add and remove elements
        void push_back(const std::string &t);
        void pop_back();
    private:
        std::shared_ptr<std::vector<std::string>> data;
};


// push_back
void StrBlob::push_back(const std::string &t) const {
    data->push_back(t);
}

//pop_back
void StrBlob::pop_back() const {
    data->pop_back();
}

I tried making push_back & pop_back const functions, and yet I can add/remove elements from the const objects using push_back/pop_back.

I don't see any reason for making those functions const, because we are able to add/remove elements from const objects anyways.

Please help me to arrive at correct answer to this.

Aucun commentaire:

Enregistrer un commentaire