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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T00:45:29+00:00 2026-05-23T00:45:29+00:00

In Chapter 24.3.4 of the book The C++ Programming Language it said that class

  • 0

In Chapter 24.3.4 of the book The C++ Programming Language it said that

class Cfield : public Field{ /*...*/ }

This expresses the notion that a
Cfield really is a kind of Field,
allows notational convenience when
writing a Cfield function that uses a
member of the Field part of the
Cfield, and – most importantly –
allows a Cfield to override Field
virtual functions. The snag is that
the Cfield* to Field* conversion
implied in the declaration of Cfield
defeats all attempts to control access
to the Field:

void g(Cfield* p)
{
    *p = "asdf"; // access to Field controlled by Cfield's assignment operator:
                 // p->Cfield::operator=("asdf")

    Field* q = p; // implicit Cfield* to Field* conversion
    *q = "asdf"   // OOPS! no control
}

What I DO NOT understand here, is the bolded sentence. How did Cfield defeat the attempt to control access to Field?

Actually, the last line of the code:

*q = "asdf"; 

will call Field::operator=("asdf"), so how did Cfield defeat the attempt to control access to Field?

  • 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-23T00:45:29+00:00Added an answer on May 23, 2026 at 12:45 am

    Let’s give a concrete example:

    struct Field {
      Field(char const* s): string(s) {}
      char const* string;
    };
    
    struct CField: Field {
      CField(char const* s): Field(s), length(s ? ::strlen(s) : 0) {}
      std::size_t length;
    };
    

    This is a very basic kind of string, that does not allow modification of the string it refers too. CField augments the Field class by caching the length of the string.

    Now, in action:

    void foo(CField& cf) {  // 0
      cf = "foo";           // 1
    
      Field& f = cf;        // 2
      f = "foobar";         // 3
    }
    

    What happens ?

    • line 0: state is unknown
    • line 1: call to CField& CField::operator=(CField const&), which creates a temporary CField (using the constructor), cf.string is "foo" and cf.length is 3
    • line 2: same object, access via Field directly
    • line 3: call to Field& Field::operator=(Field const&), which creates a temporary Field (using the constructor), cf.string is "foobar" and cf.length is 3 (unchanged)

    As you can note, the invariant that length caches the length of the string is broken because of the unprotected access to string via Field&.

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

Sidebar

Related Questions

This is a sample from book Cocoa Programming For Mac Os X 3rd(HD) chapter
I am reading the book The Java Programming Language . In the chapter which
In the Bjarne Stroustrup C++ Book (chapter 13, page 331), it said that a
I am studying the book Beginning Linux Programming 4th ed and chapter 2 is
My database looks like this: Book,Chapter,Verse,Scripture 1,1,1,1text1 1,1,2,1text2 1,1,3,1text3 1,1,4,1text4 1,2,1,2text1 1,2,2,2text2 1,2,3,2text3 I
Guys from book C++ GUI programming with qt I'm reading a chapter on how
In Chapter 9 of Programming In Scala, there is an example method like this:
In the book Programming In Scala, chapter 23, the author give an example like:
This is taken right from The C++ Programming Language by Bjarne Stroustrup. I would
I bought a C book called The C (ANSI C) PROGRAMMING LANGUAGE to try

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.