Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6082949
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T11:20:46+00:00 2026-05-23T11:20:46+00:00

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

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T11:20:46+00:00Added an answer on May 23, 2026 at 11:20 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know this sounds like an easy question but for some reason I just
I know this sounds like a dumb question but I need to ask this.
I know the question sounds silly, but consider this: I have an array of
I know this sounds like something I can google, but the truth is that
I know this sounds like a stupid question, but I really don't see the
I know this sounds like a broad question but I can narrow it down
I know this sounds like a simple question, but I just wasted the last
I know this sounds like a dumb question, but it's to settle an argument
This might sound like a strange question, but does anybody know how to force
I know this sounds like a stupid question, but here it is: Is there

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.