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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T07:00:26+00:00 2026-05-11T07:00:26+00:00

I wrote a little lazy vector class (or, delayed vector) which is supposed to

  • 0

I wrote a little ‘lazy vector’ class (or, delayed vector) which is supposed to look like a std::vector and usable wherever a std::vector is used, but it loads its elements ‘lazily’, i.e. it will load element n (and possibly a few more) from disk whenever someone accesses element n. (The reason is that in my app, not all elements fit into memory.)

Here is this LazyVector class, but there is a problem with const member functions that use such a vector, see below.

template<class T> class LazyVector {   std::vector<T> elems_;   void fetchElem(unsigned n){     // load the n-th elem from disk into elems_ etc   } public:   const T& operator[](unsigned n) const {     fetchElem(n); // ERROR: ... discards qualifiers            return elems_[n];   }   T& operator[](unsigned n) {     fetchElem(n);     return elems_[n];   }   // and provide some other std::vector functions }; 

As I said, there is a problem when a const member function asks for an element of the LazyVector. By nature of the LazyVector, accessing an element is not const, i.e. it will change the vector vec below, which is forbidden in this context. The foo member function must be const and cannot be changed. How can I solve this?

class Foo {   LazyVector<const std::string*> vec;   void fct(int n) const { // fct must be const      const std::string* str = vec[n];     // do something with str    } }; 
  • 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. 2026-05-11T07:00:27+00:00Added an answer on May 11, 2026 at 7:00 am

    You can either use mutable member data or const_cast in the implementation of your LazyVector class. Thus you can create the illusion of constness needed by your consuming class without actually being const.

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

Sidebar

Ask A Question

Stats

  • Questions 145k
  • Answers 145k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You could try the follwing 1 Do not set the… May 12, 2026 at 8:49 am
  • Editorial Team
    Editorial Team added an answer Link Testing would be the initial testing that starts at… May 12, 2026 at 8:49 am
  • Editorial Team
    Editorial Team added an answer Put this Meta tag on pages you don't want the… May 12, 2026 at 8:49 am

Related Questions

I need to write a temporary Web-based graphical front-end for a custom server system.
I'm writing yet another ActiveRecord implementation for a company that is less scared of
We have a large (about 580,000 loc) application which in Delphi 2006 builds (on
Lets say I have this extention method: public static bool HasFive<T>(this IEnumerable<T> subjects) {

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.