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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T07:21:12+00:00 2026-06-08T07:21:12+00:00

I am experimenting with const_cast operator and trying to override a const status of

  • 0

I am experimenting with const_cast operator and trying to override a const status of the parameter o passed as an argument:

void function1 (const Object *o)
{
    ...
    function2( const_cast < Object *> ( o ) ); //Exception in g++
}

void function2 (Object *o) {}

But overriding a const status of o throws an exception in g++ (GNU/Linux), in VS 2010 (Win) it works well …

Is there any more reliable way how to override a const status of a function parameter?

Update:

MSDN write: You cannot use the const_cast operator to directly override a constant variable’s constant status :-(.

  • 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-08T07:21:15+00:00Added an answer on June 8, 2026 at 7:21 am

    MSDN write: You cannot use the const_cast operator to directly override a constant variable’s constant status :-(.

    const_cast allows you to strip const specifier from the pointer, but it does not affect the “const status” of the value itself. Compiler might decide to put the value into read-only memory (well, it’s const!) and then an attempt to modify it, even through const_cast, might result in access violation.

    Here is a code snippet:

    static const int A = 1; // it's constant, it might appear on a read-only memory page
    static int B = 2; // it's a regular variable
    
    const int* pA = &A; // a const pointer
    int* pB1 = &B; // a pointer
    const int* pB2 = &B; // a const pointer to regular variable
    
    *pA = 0; // Compiler error, we are not allowed to modify const
    *const_cast<int*>(pA) = 1; // Runtime error, although const specifier is stripped from the variable, it is still on read-only memory and is not available for updates
    *pB1 = 2; // OK
    *pB2 = 3; // Compiler error, we are not allowed to modify const
    *const_cast<int*>(pB2) = 4; // OK, we stripped const specifier and unlocked update, and the value is availalbe for update too because it is a regular variable
    

    That is, const_cast removes const specifier during compile time, however it is not its purpose, authority or design to alter access mode of the underlying memory.

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

Sidebar

Related Questions

Im experimenting with the following code private void timer1_Tick(object sender, EventArgs e) { Thread
Experimenting with Scala... I'm trying to define something analogous to the @ hack in
i am experimenting with overloading the < operator (i'll add the > later) as
I've been experimenting with variadic templates and argument forwarding. I think I've found some
I'm experimenting with ZeroMQ and trying to get something working. My first thought was
I'm researching and experimenting more with Groovy and I'm trying to wrap my mind
I run this code for experimenting copy constructor and assignment operator class AClass {
Experimenting with jQuery and trying to make a small slide show that rotates through
I'm experimenting with hosting CLR (not trying to use mono for now, though I
Experimenting with android game development. I'm using PNG format for images, and was wondering

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.