简体   繁体   中英

how to modify private members that are const?

I know this sounds like a strange question, but bear with me.

I have a custom class that has some large objects which need to be returned by reference to avoid a copy.

My class looks something like 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 for access is:

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

When I try to compile this, I get an error, as it wants

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

In practice, storage is logically const, but I just need to be able to modify it once in a while. Is there a way to do this?

Also, a related question. If I call modify_element to modify an element in storage, but previously, I have returned a reference to that element using access, will the reference that was returned previously pick up the new value after the modify_element call?

After fixing up your program, and with the hint from @user763305, I suppose you have something like this (note: this program compiles, but invokes undefined behavior when run, since the storage vector is 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 you get an error like this:

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

So, you have a const object and are trying to invoke modify_element on it. Logically, this is a mistake -- you can't modify const objects!

You have, as I see it, two choices for a fix. Either you can declare the vector object mutable and the modify_element method const , or you can modify your object factory to 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 you previously returned a reference to a string in your vector , and you subsequently assign a new value to that string as you do in modify_element , then the previous reference will reflect the new value.

Note that the previous reference will be invalid if you destroy the vector, erase that vector element, or do anything to cause the vector to grow beyond its previous capacity.

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