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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T14:55:20+00:00 2026-06-15T14:55:20+00:00

I have a class Reader which provides access to a binary file to perform

  • 0

I have a class Reader which provides access to a binary file to perform read operations on it.

This file contains several lists of offsets within the same file, where data is to be found. This means that, to get to certain data segments, one has to read an offset from some position, then jump to that offset, read an offset again, jump there, etc. until finally you reach the actual data.

The class maintains a reading position that is adjusted each time you call a method like

// reads 4 bytes and advances the position by 4 bytes
uint32 Reader::readOffset() { /* */ } 

or

// moves the position to offset
void Reader::jumpTo(uint32 offset) { /* */ }

Those methods obviously cannot be const as they are moving the reading position.

For convenient navigation between several levels of the file, the class provides a stack, where one can push and pop offsets as needed:

uint32 someOffset = reader.readOffset();
reader.pushOffset(); // remember position
reader.jumpTo(someOffset); // do something on another position
reader.popOffset(); // go back to where we were before

Those push/pop methods cannot be const as well, because they alter the offset stack.

The problem now is with convenience methods, that are supposed to extract data from an internally known position within the file. They have to work regardless of the current reading position and should not touch it. By design, they ought to be const, but this does not work:

uint32 Reader::readSomeDataFromKnownLocation() const
{
    pushOffset();

    jumpTo(1234ul);
    uint32 data = readData();

    popOffset();

    return data;
}

I do know that this method will leave the object in the same state as it was before the call, but still I cannot make it const because each and every method I use within it is non-const.

So, my question is, what is the best approach when having to temporarily change an object’s state within a “by-design” const method?

I thought about const_cast<Reader*>(this) but this seems like a hackish approach. Or do you think it is justified in this scenario?

  • 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-15T14:55:21+00:00Added an answer on June 15, 2026 at 2:55 pm

    You can make the position field mutable and then mark your logically read only methods as const.

    If you want to keep the pushOffset and popOffset methods in the public API, you should introduce private versions that are const, and use the private ones from within your readSomeDataFromKnownLocation method.

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

Sidebar

Related Questions

I have a pymzml.run.Reader class from the pymzml package. This is a generator object,
I have an XML reader class which I initialize with a URL - (id)initWithURL:(NSURL
I have a class that is a subclass of UITabBarController which contains 4 child
I have the following class which I am using to read in large amounts
I have this code: class DocumentIdentifier attr_reader :folder, :name def initialize( folder, name )
I have the following methods: public <T> T fromJson( Reader jsonData, Class<T> clazz )
I have Class and Student objects. Both have collection of another as property. Which
I have class LegacyClass which inherits OldBaseClass. I'm considering a change to introduce a
Lets say I have a controller which provides search functions for a student: public
I have a zooming UIScrollView that contains a UIView which contains some custom UIViews

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.