[RESOLVED] How does const_cast<> work?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: [RESOLVED] How does const_cast<> work?

  1. #1
    Join Date
    Jan 2012
    Location
    USA
    Posts
    89

    [RESOLVED] How does const_cast<> work?

    Hello,

    const_cast<> supposed to "cast away const" but it does not. The output is: 1002 1001, I expect it to be 1002 1002. Could someone explain what's going on? Thx.

    Code:
    #include<iostream>
    using namespace std;
    
    int main(int argc, char* argv[])
    {
        const int ci = 1001;
        int* i1 = const_cast<int*>(&ci);
        *i1 = 1002;
        cout << *i1 << " " << ci << endl;
    
        return 0;
    }

  2. #2
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,012

    Re: How does const_cast<> work?

    Quote Originally Posted by vincegata View Post
    const_cast<> supposed to "cast away const" but it does not. The output is: 1002 1001, I expect it to be 1002 1002. Could someone explain what's going on? Thx.

    Code:
    #include<iostream>
    using namespace std;
    
    int main(int argc, char* argv[])
    {
        const int ci = 1001;
        int* i1 = const_cast<int*>(&ci);
        *i1 = 1002;
        cout << *i1 << " " << ci << endl;
    
        return 0;
    }
    Your program is ill-formed. According to the standard, you are not allowed to assign to the result of a const_cast when the original is defined as const. "Casting away const" just means that you are circumventing the type system. You are not changing any actual characteristics of the data in your program. The only real use for it is when you need to use a C interface that does not support const. But you should only use it when you are sure that the result of the const_cast will not be written to.
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  3. #3
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    3,827

    Re: How does const_cast<> work?

    const_cast isn't diffent from the C-style of casting away a const.

    it's the programmer's responsability to make sure that removing the constness is actually safe.
    Countrary to D_Drmmr's statment, you CAN write to a object that has it's const removed. But only if you can guarantee safety thereof yourself.

    you cannot safely use it to write to a variable that was defined as const because said variable may be in a readonly memorypage (or ROM).
    writing could also have an impact on optimisations, the code "outside" of the const cast bit may very well assume that no changes can happen and the compiler may use that to optimize code according to that. writing to it anyway could have all kinds of undesired effects.

    const_cast overrides the compiler, you're responsible for what happens.

  4. #4
    Join Date
    Apr 1999
    Posts
    27,424

    Re: How does const_cast<> work?

    Quote Originally Posted by vincegata View Post
    Hello,

    const_cast<> supposed to "cast away const" but it does not. The output is: 1002 1001, I expect it to be 1002 1002. Could someone explain what's going on? Thx.
    To add, try changing that to a string-literal instead of an int, cast the constness away, and then change it. You will see that your program will more than likely crash.
    Code:
    #include<iostream>
    using namespace std;
    
    int main(int argc, char* argv[])
    {
        const char * ci = "1001";
        char*  i1 = const_cast<char*>(ci);
        *i1 = '8';
        return 0;
    }
    What is expected to happen is that the first character should be an '8' instead of '1', right? You'll be lucky if you even get to the return statement at the end.

    Regards,

    Paul McKenzie

  5. #5
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,261

    Re: How does const_cast<> work?

    Quote Originally Posted by OReubens
    Countrary to D_Drmmr's statment, you CAN write to a object that has it's const removed.
    The way I interpreted it, D_Drmmr did not say that you cannot "write to a object that has it's const removed". Rather, the statement was that "you should only use it when you are sure that the result of the const_cast will not be written to" (emphasis mine). This makes sense to me because if the const-ness was introduced somewhere, then there would have been an implied promise that the object would not be written to in that context. As such, I agree with D_Drmmr that the common use is when the object is passed to a C (or sometimes even C++) API that is not const-correct, but which say, effectively guarantees the const-ness in the documentation.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  6. #6
    Join Date
    Jan 2012
    Location
    USA
    Posts
    89

    Re: How does const_cast<> work?

    Thanks for replies.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center