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 8879865
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T20:02:53+00:00 2026-06-14T20:02:53+00:00

I am creating a class that is similar to a vector. It uses its

  • 0

I am creating a class that is similar to a vector. It uses its own algorithm to determine where and how the data is actually stored. From the user’s standpoint, it works just like a normal vector.

Internally, it tries to save memory by not storing duplicate values more than once. If the same value is pushed onto the vector more than once, it does some fancy dance moves with the indices but only stores the actual value once.

When overloading operator[], the index argument is converted to the “real” index so it can access where the data is actually stored.

If the user tries to access an element like so:

int i = svec[8];

The correct element is returned by converting 8 to the “real” index and accessing the correct element.

The problem comes in when the user tries to assign a value like so:

svec[8] = 1;

The value 8 will again be covered to the “real” index and a reference to the element is returned by operator[](). However, multiple indices might be mapping to that element. If I return a reference and allow it to be changed, it will have the undesired side effect of changing the value for other indices.

If I just return the element by value, you won’t be able to assign a value using operator[].

Within operator[](), I would like to know if an operation plans to change the value of the element so I can create a new element, do some more fancy dance moves with the indices, and return a reference to the new element.

Is anything like this possible? If not, it seems like I’m forced to return by value, thereby preventing the user from modifying the values using [].

  • 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-06-14T20:02:55+00:00Added an answer on June 14, 2026 at 8:02 pm

    It would be easy, if the class would have a method change_element and everybody is calling this function when changing, isn’t it?

    template <typename T>
    struct my_vec {
      T& change_element(int index, T new_value)
      { ... }
    };
    

    Now, the idea is to return a proxy class from operator[] that does this job for you.

      // inside my_vec:
      struct proxy {
        my_vec *vec;
        int index;
    
        T& operator=(T o)
        { return vec->change_element(index, std::move(o)); }
    
        operator T() const { return (*const_cast<my_vec const*>(vec))[index]; }
      };
    
      proxy operator[](int i) { return proxy{this, i}; }
      T const& operator[](int i) const { ... }
    

    There is no overhead, every common compiler should optimize the proxy element away.

    This trick is even used in the STL by vector<bool>. Check this out for a more complete example.

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

Sidebar

Related Questions

I'm creating a class that derives from List... public class MyList : List<MyListItem> {}
I am creating a batching class that uses VAOs and VBOs to manage meshes.
I'm creating a new class that inherits from UIView. I remember that xcode 3
I'm creating a new class that inherits queue from the STL library. The only
I'm creating a container class very similar to a vector (class project), and have
I m creating a class that extends AlertDialog. and setting layout. But when pop
I am creating a class that is mapped to an existing global so it
I'm creating a class that creates arrays. (Yes, I realize that's already available in
I am creating a class that implements java.util.List . My class is using E
I'm creating a class that will have one public method, which returns a value

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.