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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:08:44+00:00 2026-05-25T11:08:44+00:00

I read on the wikipedia page for Null_pointer that Bjarne Stroustrup suggested defining NULL

  • 0

I read on the wikipedia page for Null_pointer that Bjarne Stroustrup suggested defining NULL as

const int NULL = 0;

if “you feel you must define NULL.” I instantly thought, hey.. wait a minute, what about const_cast?

After some experimenting, I found that

int main() {
    const int MyNull = 0;
    const int*  ToNull = &MyNull;
    int* myptr = const_cast<int*>(ToNull);
    *myptr = 5;
    printf("MyNull is %d\n", MyNull);
    return 0;
}

would print “MyNull is 0”, but if I make the const int belong to a class:

class test {
public:
    test() : p(0) { }
    const int p;
};
int main() {
    test t;
    const int* pptr = &(t.p);
    int* myptr = const_cast<int*>(pptr);
    *myptr = 5;
    printf("t.p is %d\n", t.p);
    return 0;
}

then it prints “t.p is 5”!

Why is there a difference between the two? Why is “*myptr = 5;” silently failing in my first example, and what action is it performing, if any?

  • 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-25T11:08:45+00:00Added an answer on May 25, 2026 at 11:08 am

    Your code is modifying a variable declared constant so anything can happen. Discussing why a certain thing happens instead of another one is completely pointless unless you are discussing about unportable compiler internals issues… from a C++ point of view that code simply doesn’t have any sense.

    About const_cast one important thing to understand is that const cast is not for messing about variables declared constant but about references and pointers declared constant.

    In C++ a const int * is often understood to be a “pointer to a constant integer” while this description is completely wrong. For the compiler it’s instead something quite different: a “pointer that cannot be used for writing to an integer object”.

    This may apparently seem a minor difference but indeed is a huge one because

    1. The “constness” is a property of the pointer, not of the pointed-to object.

    2. Nothing is said about the fact that the pointed to object is constant or not.

    3. The word “constant” has nothing to do with the meaning (this is why I think that using const it was a bad naming choice). const int * is not talking about constness of anything but only about “read only” or “read/write”.

    const_cast allows you to convert between pointers and references that can be used for writing and pointer or references that cannot because they are “read only”. The pointed to object is never part of this process and the standard simply says that it’s legal to take a const pointer and using it for writing after “casting away” const-ness but only if the pointed to object has not been declared constant.

    Constness of a pointer and a reference never affects the machine code that will be generated by a compiler (another common misconception is that a compiler can produce better code if const references and pointers are used, but this is total bogus… for the optimizer a const reference and a const pointer are just a reference and a pointer).

    Constness of pointers and references has been introduced to help programmers, not optmizers (btw I think that this alleged help for programmers is also quite questionable, but that’s another story).

    const_cast is a weapon that helps programmers fighting with broken const-ness declarations of pointers and references (e.g. in libraries) and with the broken very concept of constness of references and pointers (before mutable for example casting away constness was the only reasonable solution in many real life programs).

    Misunderstanding of what is a const reference is also at the base of a very common C++ antipattern (used even in the standard library) that says that passing a const reference is a smart way to pass a value. See this answer for more details.

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

Sidebar

Related Questions

From the Wikipedia article on Read-Copy-Update : The reason that it is safe to
Possible Duplicate: What is dependency injection? I've read the Wikipedia page. I've read an
I've read in Wikipedia that neural-network functions defined on a field of arbitrary real/rational
I stumbled across the Wikipedia page for them: Fusion tree And I read the
I've read "what-is-turing-complete" and the wikipedia page, but I'm less interested in a formal
I read the Wikipedia article on scenario testing, but I am sad to say
I've read the Wikipedia articles for both procedural programming and functional programming , but
I just read the Wikipedia article on mock objects , but I'm still not
I've read the Wikipedia article on reactive programming . I've also read the small
In 2 complements I read from wikipedia, the range is from -128 to 127.

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.