c++ - how to modify private members that are const? -


i know sounds strange question, bear me.

i have custom class has large objects need returned reference avoid copy.

my class looks this:

    class csv_file {     public:       csv_file(std::string); //constructor       std::string const& access(int,int) const;       void modify_element(int column,int row ,std::string value) {         storage.at(row).at(column)=value;     }      private:       mutable std::vector < std::vector<std::string> > storage;      }; 

the code access is:

    string const& csv_file::access(int column,int row) const {     return storage.at(row).at(column);     } 

when try compile this, error, wants

csv_file::modify_element(int column,int row ,std::string value) const {} 

in practice, storage logically const, need able modify once in while. there way this?

also, related question. if call modify_element modify element in storage, previously, have returned reference element using access, reference returned pick new value after modify_element call?

after fixing program, , hint @user763305, suppose have (note: program compiles, invokes undefined behavior when run, since storage vector left empty):

#include <string> #include <vector>  class csv_file { public:   csv_file(std::string) {}   std::string const& access(int,int) const;   void modify_element(int column,int row ,std::string value) {     storage.at(row).at(column)=value;   }  private:   mutable std::vector < std::vector<std::string> > storage;  };  std::string const& csv_file::access(int column,int row) const {   return storage.at(row).at(column); }  const csv_file& factory() { static csv_file foo("foo.txt"); return foo; } int main(){     const csv_file& f(factory());     f.modify_element(1, 2, "hello"); } 

and error this:

cs.cc: in function ‘int main()’: cs.cc:24: error: passing ‘const csv_file’ ‘this’ argument of ‘void csv_file::modify_element(int, int, std::string)’ discards qualifiers 

so, have const object , trying invoke modify_element on it. logically, mistake -- can't modify const objects!

you have, see it, 2 choices fix. either can declare vector object mutable , modify_element method const, or can modify object factory return non-const references.

solution 1:

...   void modify_element(int column,int row ,std::string value) const {     storage.at(row).at(column)=value;   } ... 

solution 2:

... const csv_file& factory() { static csv_file foo("foo.txt"); return foo; } csv_file& rw_factory() { static csv_file foo("foo.txt"); return foo; } ...   const csv_file& f(rw_factory()); ... 


edit and, yes, if returned reference string in vector, , subsequently assign new value string in modify_element, previous reference reflect new value.

note previous reference invalid if destroy vector, erase vector element, or cause vector grow beyond previous capacity.


Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -