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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T13:11:48+00:00 2026-05-20T13:11:48+00:00

In the book of "The C++ Programming Language", the author gave the following example

  • 0

In the book of "The C++ Programming Language", the author gave the following example along with several statements:

Defining an operator, such as [], to be used for both reading and writing is difficult where it is not acceptable simply to return a reference and let the user decide what to do with it.

Cref, is to help implement a subscript operator that distinguishes between reading and writing.

Why [] is difficult to be defined when to be used for both reading and writing?
How does the definition of class Cref help to solve this issue?

  class String{
     struct Srep; 
     Srep *rep; 

     public: 
        class Cref;
        
     // some definitions here
     void check (int i) const { if (i<0 || rep->sz<=i) throw Range( );}
     
     char read( int i) const {return rep->s[i];}
     void write(int i, char c){ rep=rep->get_own_copy(); rep->s[i]=c;}

     Cref operator[] (int i){ check(i); return Cref(*this, i);}
     char operator[] (int i) const{check(i); return rep->s{i];}

   }

  class String::Cref{
    friend class String;
          String& s;
          int i;
          Cref(String& ss, int ii): s(ss),i(ii) {}
    public:
          operator char( ) { return s.read(i);}
          void operator=(char c){s.write(i,c);}
  };
  • 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-20T13:11:49+00:00Added an answer on May 20, 2026 at 1:11 pm

    If you don’t define a class Cref that solves this issue, then you have to do what std::map does:

    template class <K,V> class map{
       V& operator[](K const & key);
    }
    

    This returns a reference, which must be backed by a valid memory location, and therefore

    std::map<string,string> m;
    m["foo"];
    assert(m.find("foo") != m.end());
    

    The assertion will succeed (meaning, "foo" is now a valid key in the map) even though you never assigned something to m["foo"].

    This counterintuitive behavior can be fixed by the Cref class in your example — it can perform the appropriate logic to create m["foo"] only when you assign to the reference, and ensure that m.find("foo") == m.end() if you didn’t perform some assignment when you tried to read the nonexistant m["foo"].

    Likewise, in your String class (which is a reference-counted string — strings share their string data, and a new copy is created when you change a string whose data is shared with another string), you’d have to make a copy when using operator[] to read characters. The use of the Cref class, allows you to ensure that you only make a copy when using operator[] to write.

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

Sidebar

Related Questions

In a beginner's programming book (free licence) there was the following code, dynamically creating
I'm reading this book: The C Programming Language - By Kernighan and Ritchie (second
In the book "Unix Programming Environment" the book relies on writing its own lexer
I am currently reading the book called "Programming ActionScript 3.0" available from Adobe. After
Each book can have many authors. And each author can author many books. class
One Book(Object Oriented Programming with C++ by E.Balagurusamy) says that const size = 10;
I'm preparing the SCJP exam with the almost official study book ("SCJP Sun Certified
I am reading the book "Exceptional C++" by Herb Sutter, and in that book
I'm following Pro ASP.Net MVC2 book and literally 90% of everything is new to
I am trying to work out an example from ".NET Domain Driven Design with

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.