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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T15:54:05+00:00 2026-05-20T15:54:05+00:00

In the book of The C++ programming language, author gave the following example. He

  • 0

In the book of “The C++ programming language”, author gave the following example. He mentioned that ” the cache needs to be filled before it can be used”. It seems to me that this is why the function of compute_cache_value is placed. But I do not understand what does the function of string_rep( ) do in accordance with its implementation. Thanks for clarification.

class Date{
      bool cache_valid;
      string cache;
      void compute_cache_value( );  //fill cache
      // ...
public: 
      // ...
     string string_rep( ) const;
};

string Date:: string_rep( ) const
{
    if (cache_valid == false) {
          Date* th = const_cast<Date*> (this); // cast away const
          th->compute_cache_value( );
          th->cache_valid = true;
    }
    return cache;
}

In addition, the author gave the following for examples:

Date d1;
const Date d2;
string s1 = d1.string_rep( );
string s2 = d2.string_rep( );

And the author stated that the fourth example will display undefined behavior. I would like to know why.

  • 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-20T15:54:05+00:00Added an answer on May 20, 2026 at 3:54 pm

    string_rep checks to see if there’s a cached string representation of the date. If not, it calls the compute_cache_value method to create the string representation and cache it, and then marks the cache representation as valid so future calls to string_rep don’t recalculate it.

    The line const Date d2; will display undefined behavior because the compiler may assume it can put d2 in nonvolatile memory (e.g. flash in a microcontroller, or read-only-flagged memory or memory-mapped memory), and the const_cast<Date*> may not work in that case, resulting in cache_valid staying false or cache staying as an empty string.

    P.S. As Michael J below points out below, there are almost always better ways to do things than using const_cast. In the case of this example, the mutable keyword comes in handy. In short, marking a class member mutable tells the compiler that the member may be changed even if the object is const.

    class Date
    {
          mutable bool cache_valid;
          mutable string cache;
          void compute_cache_value() const;
          // ...
    public: 
          // ...
         string string_rep() const;
    };
    
    string Date::string_rep() const
    {
        if (cache_valid == false) {
              compute_cache_value();
              cache_valid = true;
        }
        return cache;
    }
    

    … though I’d argue that it’s compute_cache_value‘s responsibility to set cache_valid, and I’d consider adding operator string() const { return string_rep(); } to Date.

    One last thing that’s neat about mutable is that the compiler has a better idea of what is going on, and in a case like d2, can put the object in volatile memory despite its declaration as const.

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

Sidebar

Related Questions

In the book of The C++ Programming Language, the author gives the following example
I saw the following example in book of the C++ Programming Language class Ptr
In section 7.1.1 of the book The C++ Programming Language the author states: inline
In Bjarne Stroustrup's book The C++ Programming Language, it is stated that a derived
In the book 'Code Complete' the author talks about programming into a language (instead
In the book The C Programming Language it says: Many of the functions in
I have been reading from the book The C Programming Language learning C, and
I bought The D Programming Language a little while ago. Great book, very educational.
In the book Programming Collective Intelligence I found the following function to compute the
I need a programming book with questions and answers, doesn't matter what programming language

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.