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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T14:30:02+00:00 2026-06-07T14:30:02+00:00

From my understanding , mutable cancels the constness of a variable Class A {

  • 0

From my understanding , mutable cancels the constness of a variable

Class A {
  void foo() const {
  m_a = 5;
}
mutable int m_a;
};

But also const_cast :

void print (char * str)
{
  cout << str << endl;
}

int main () {
  const char * c = "this is a line";
  print ( const_cast<char *> (c) );
  return 0;
}

So , what changes one from the other ?

Thanks

  • 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-07T14:30:05+00:00Added an answer on June 7, 2026 at 2:30 pm

    const_cast cannot cancel constness of an object. const_cast can only remove constness from an access path to an object. Access path is a pointer or a reference to an object. Removing the constness from the access path has absolutely no effect on the object itself. Even if you use const_cast to remove the constness of the access path, that still does not necessarily give you the permission to modify the object. Whether you can do it or not still depends on the object itself. If it is const, you are not allowed to modify it and any attempts to do so will result in undefined behavior.

    For example, this illustrates the intended use of const_cast

      int i = 5; // non-constant object
      const int *p = &i; // `p` is a const access path to `i`
    
      // Since we know that `i` is not a const, we can remove constness...
      int *q = const_cast<int *>(p);
      // ... and legally modify `i`
      *q = 10;
      // Now `i` is 10
    

    The only reason the above is legal and valid is the fact that i is actually a non-constant object, and we know about it.

    If the original object was really constant, then the above code would produce undefined behavior:

      const int j = 5; // constant object
      const int *p = &j; // `p` is a const access path to `j`
    
      int *q = const_cast<int *>(p); // `q` is a non-const access path to `j`
      *q = 10; // UNDEFINED BEHAVIOR !!!
    

    C++ language does not allow you to modify constant objects and const_cast is completely powerless here, regardless of how you use it.

    mutable is a completely different thing. mutable creates a data filed that can be legally modified even if the containing object is declared const. In that sense mutable does allow you to modify [some designated parts of] constant objects. const_cast, on the other hand, can’t do anything like that.

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

Sidebar

Related Questions

From my understanding, each of these methods: get() and put() are atomic. But, when
From my understanding partial classes are a bit frowned upon by professional developers, but
From my understanding $_COOKIE gives me all the cookies in a user's browser. But
From my understanding, the virtual keyword allows you to use the base class' method,
From my understanding of the mutable keyword, one of its primary uses is caching
Continuing the discussion from Understanding VS2010 C# parallel profiling results but more to the
From my understanding the Adapter pattern is basically creating a wrapper on another class
Based on the understanding from the following: Where is allocated variable reference, in stack
From my understanding, const modifiers should be read from right to left. From that,
I might be mis-understanding here but from my understanding a markdown editor strips out

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.