c++ - const member function mutable -


as given understand, const specifier @ end of member function means class's members cannot modified, within function, unless declared mutable. having said have following:

mns::list::list linked list implementation of mine functions not const.

template<typename t> class hashtable{ private:       std::vector<mns::list::list<t> * > * hashtable;       long long int size;  public:         hashtable(int _size){              hashtable = new std::vector<mns::list::list<t> * >(_size, null);              size = _size;        }         ....         bool insert(t _value) const {              long long int hash_value = gethash(_value);               if (hashtable->at(hash_value) == null) hashtable->at(hash_value) = new mns::list::list<t>(_value);              else if (hashtable->at(hash_value)->find_forward(_value) == -1) hashtable->at(hash_value)->push_top(_value);        }         .... } 

and have function in main:

void testhash(){        hashtable<int> testhashtable1(100);        std::cout << testhashtable1.find(45) << std::endl;        testhashtable1.insert(45);        std::cout << testhashtable1.find(45) << std::endl; } 

given hashtable::insert const function assumed wouldn't able modify private member hashtable. testhash() prints:

0

1

which means member modified... right? seem miss here... why private member vector hashtable modified?

your data member pointer vector. in const member function insert data members const, data member considered const pointer vector. since not try modify pointer (e.g. hashtable = nullptr) vector pointed it, vector modified.


Comments

Popular posts from this blog

account - Script error login visual studio DefaultLogin_PCore.js -

xcode - CocoaPod Storyboard error: -