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

  • Home
  • SEARCH
  • 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 1032337
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T14:03:46+00:00 2026-05-16T14:03:46+00:00

Consider the task of writing an indexable class which automatically synchronizes its state with

  • 0

Consider the task of writing an indexable class which automatically synchronizes its state with some external data-store (e.g. a file). In order to do this the class would need to be made aware of changes to the indexed value which might occur. Unfortunately the usual approach to overloading operator[] does not allow for this, for example…

Type& operator[](int index)
{
    assert(index >=0 && index < size);
    return state[index];
}

I there any way to distinguish between a value being accessed and a value being modified?

Type a = myIndexable[2]; //Access
myIndexable[3] = a;  //Modification

Both of these cases occur after the function has returned. Is there some other approach to overloading operator[] which would perhaps make more sense?

  • 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-16T14:03:46+00:00Added an answer on May 16, 2026 at 2:03 pm

    From the operator[] you can only really tell access.
    Even if the external entity uses the non cost version this does not mean that a write will take place rather that it could take place.

    As such What you need to do is return an object that can detect modification.
    The best way to do this is to wrap the object with a class that overrides the operator=. This wrapper can then inform the store when the object has been updated. You would also want to override the operator Type (cast) so that a const version of the object can be retrieved for read accesses.

    Then we could do something like this:

    class WriteCheck;
    class Store
    {
      public:
      Type const& operator[](int index) const
      {
        return state[index];
      } 
      WriteCheck operator[](int index);
      void stateUpdate(int index)
      {
            // Called when a particular index has been updated.
      }
      // Stuff
    };
    
    class WriteCheck
    { 
        Store&  store;
        Type&   object;
        int     index;
    
        public: WriteCheck(Store& s, Type& o, int i): store(s), object(o), index(i) {}
    
        // When assignment is done assign
        // Then inform the store.
        WriteCheck& operator=(Type const& rhs)
        {
            object = rhs;
            store.stateUpdate(index);
        }
    
        // Still allow the base object to be read
        // From within this wrapper.
        operator Type const&()
        {
            return object;
        }   
    };      
    
    WriteCheck Store::operator[](int index)
    {   
        return WriteCheck(*this, state[index], index);
    }
    

    An simpler alternative is:
    Rather than provide the operator[] you provide a specific set method on the store object and only provide read access through the operator[]

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

Sidebar

Related Questions

After having read Ian Boyd 's constructor series questions ( 1 , 2 ,

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.