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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:22:27+00:00 2026-05-28T07:22:27+00:00

I have the following code : const int k=1; int *p=const_cast<int *>( &k); cout<<k

  • 0

I have the following code :

const int k=1;
    int *p=const_cast<int *>( &k);
    cout<<"k before="<<*p<<endl;
    *p=10;
    *const_cast<int *>( &k)=12;
    cout<<"k after="<<k<<endl;

the output was :

k before=1
k after=1

why doesn’t const cast work here ?

  • 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-28T07:22:28+00:00Added an answer on May 28, 2026 at 7:22 am

    const_cast is normally used when/if you receive a const pointer to an object that wasn’t originally defined as const. If (as in your case) the object was originally defined as const, attempting to modify it causes undefined behavior. Without the const_cast, the compiler won’t let you even try to do that (the code won’t compile).

    A cast, however, tells the compiler you’re sure you know what you’re doing and it’s really safe, so the compiler just needs to shut up and do what you told it instead of giving any error/warning messages like it might usually do. Unfortunately, in this case what you’re doing is not really safe, but since you’ve told the compiler to shut up and do it, you won’t get any warning about it (at least with most compilers).

    As to what you should do, it comes down to deciding whether your k is really const or not. If you really need to modify it, then you need to define it as a normal (non-const) variable. If you want to ensure that only a small amount of specific code can modify it, then you could/can (for one possibility) make it private to a small class:

    class my_int { 
        int k;
    public:
        my_int() : k(1) {}
    
        do_mod() { k = 10; }
    
        operator int() { return k; }
    };
    

    Now, do_mod can modify k directly. Other code can use a my_int object as if it were an int, but can’t modify its value — in essence, it acts like an rvalue.

    In fairness, I should probably point out that if it really tries by doing some casting, other code can modify the value of k. As Bjarne has said, C++’s protection mechanism is intended to prevent accidents, not intentional subversion.

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

Sidebar

Related Questions

i have the following code below: const char* timeformat = %Y-%m-%d %H:%M:%S; const int
i have following code #include <stdlib.h> #include <stdio.h> int sorter( const void *first_arg,const void*
I have the following code: const int PROVIDER_RSA_FULL = 1; const string CONTAINER_NAME =
Say I have the following code snippet in c# static const bool DO_PERFORMANCE_CODE =
I have the following code that compiles and works well: template<typename T> T GetGlobal(const
I have the following code: template <class T> struct pointer { operator pointer<const T>()
I have the following: classA::FuncA() { ... code FuncB(); ... code } classA::FuncB(const char
I have the following piece of code which doesn't compile when I try to
Is the following int BlkArray::GetNthBlockA(unsigned int n, const Block *&pfb, int &maxIndex) const {
I have the following code: template<int k> void foo() { } int main(int argc,

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.