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

Related Questions

Im trying to learn glib/gtk. I wrote little code which prints files in directory
I wrote a little Python script which uses an Adder plugin to mix two
I wrote a little Python script wrapcl.py script which wraps our compiler binary (
I wrote a little graph visualizer class: public void simpleGraph(SparseMultigraph<Vertex,SEdge> graph, String name) {
Some time ago I wrote a little piece of code to ask about on
I've been playing with Python for a while and wrote a little program to
I wrote myself a little downloading application so that I could easily grab a
First a little intro: Last year i wrote this http://dragan.yourtree.org/code/canvas-3d-graph/ Now, i want to
I want to write a little app for myself which notifies me if there
This may be a silly question but... Say you have a sentence like: The

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.