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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T10:34:49+00:00 2026-05-24T10:34:49+00:00

I was re-reading c++ primer(4th ed.) today – the section on member functions and

  • 0

I was re-reading c++ primer(4th ed.) today – the section on member functions and const references etc, and I came up with this wierd little program:

using std::cout;
using std::endl;

class ConstCheater
{
public:
    ConstCheater(int avalue) : ccp(this), value(avalue) {}
    ConstCheater& getccp() const {return *ccp;}
    int value;
private:
    ConstCheater* ccp;
};

int main()
{
    const ConstCheater cc(7); //Initialize the value to 7
    cout << cc.value << endl;
    cc.getccp().value = 4;    //Now setting it to 4, even though it's const!
    cout << cc.value << endl;
    cc.value = 4;             //This is illegal
    return 0;
}

My question is – why does c++ allow syntax such as this? Why can I edit normal data members in a class when it’s declared const? Isn’t the POINT of const to make it so that you can’t modify values?

  • 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-24T10:34:49+00:00Added an answer on May 24, 2026 at 10:34 am

    I’d say the answer by Tony that you marked as correct is incorrect, and the answer by Michael Burr is correct.

    To put what he said more clearly (for me at least):

    There are two potential places for misunderstanding:

    1. The way implicit const works
    2. The way that this is interpreted during construction of a const object

    1. Implicit Const

    Implicit const (the constification internal to ConstCheater when it is made const) doesn’t turn cc into a pointer-to-const but rather a const-pointer, that is to say when you do this:

      const ConstCheater cc(7); 
    

    Internally goes from:

      ConstCheater * ccp;
    

    …to…

      ConstCheater * const ccp;
    

    …rather than the…

      const ConstCheater * ccp;    
    

    that may have been expected.

    2. Construction of const object

    The stranger thing is though is that this is allowed to be passed to cpp‘s initializer in the constructor since this, one would think, should be treated as a pointer-to-const, and thus not a valid value to pass to a const-pointer.

    That is to say one might expect:

     ...: ccp(this) ... // expected to fail but doesnt
    

    to fail because conceptually you might expect that this was (somewhat) equivalent to:

     const ConstCheater         cc(7);
     const ConstCheater * const this = &cc; // const-pointer-to-const
    

    and thus you would think that:

     ConstCheater * const ccp = this; //expected error!
    

    would fail! But it doesn’t because apparently during construction apparently this is treated specially as if it was:

     const ConstCheater * this = &cc; 
    

    and thus the object is effectively not const during construction.

    I’m not sure I understand completely the reasoning, but Michael Burr points out there appears to be a logical and technical barrier to providing the expected behavior so the standard seems to carve out the current somewhat odd behavior.

    I recently asked a related question which was: Why does C++ not have a const constructor? but thus far haven’t really understood completely the reasoning why it would be untenable, though I suppose it would place a burden on C++ developers to have to define an awkward const constructor for any class they’d like to create const object of.

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

Sidebar

Related Questions

I was reading through the c++ Primer and this code snippet came up and
Probably an extremely simple answer to this extremely simple question: I'm reading C Primer
Reading this old but classic document Writing High-Performance Managed Applications - A Primer ,
While reading C++ Primer Plus 5th edition, I saw this piece of code: cin.get(ch);
I'm relatively new to C++, I start by reading C++ Primer 4th edition .
Reading this question I found this as (note the quotation marks) code to solve
Reading over the responses to this question Disadvantages of Test Driven Development? I got
Reading on another forum I've came across the world of CSS Frameworks. The one
Reading through this question on multi-threaded javascript, I was wondering if there would be
Reading this post has left me wondering; are nightly builds ever better for a

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.