CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Jan 2012
    Posts
    17

    Got confused with const_cast

    Sample_Program-1

    #include<iostream>
    using namespace std ;

    int main(){
    const int i = 9;
    int *j = const_cast<int*>(&i); //Ok
    int *j = const_cast<int*>(i); //Error
    }

    Sample_Program-2

    #include<iostream>
    using namespace std ;

    int main(){
    const int i = 9;
    int j = const_cast<int&>(i);//Ok
    int j = const_cast<int>(i);//Error
    }


    I was just learning some c++ concept and met with the above 2 concepts . Can anyone please explain the concept i marked as error in the above 2 sample program ?

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Got confused with const_cast

    Your compiler should have given you some error messages. What do they say and what do you think they mean?
    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

  3. #3
    Join Date
    Jan 2012
    Posts
    17

    Re: Got confused with const_cast

    My compiler gives only error message but dont explain it . I am new to c++ . Thats why i have mentioned in my post "Can anyone please explain the conpt i marked as error in the above 2 sample program ?"

  4. #4
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Got confused with const_cast

    Quote Originally Posted by vikuseth
    My compiler gives only error message but dont explain it .
    Well, for your first example (once I fix an irrelevant error whereby you declare j twice), my compiler complains that there was an:
    Code:
    invalid const_cast from type 'const int' to type 'int*'
    Doesn't that explain to you what is the problem? The problem is that the const_cast is invalid. Why is it invalid? Because a const_cast is supposed to be used to remove (or add) const-ness, but here you are also changing from a non-pointer type to a pointer type.

    For your next example, I get:
    Code:
    invalid use of const_cast with type 'int', which is not a pointer, reference, nor a pointer-to-data-member type
    This tells you that the const_cast is invalid because what you are trying to cast "is not a pointer, reference, nor a pointer-to-data-member type".

    So, did you actually read the error messages that your compiler gave you and tried to make sense to what they said?
    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

  5. #5
    Join Date
    Jan 2012
    Posts
    17

    Re: Got confused with const_cast

    So constant cast only works for pointer , reference or pointer to datamember ?

  6. #6
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Got confused with const_cast

    Quote Originally Posted by vikuseth
    So constant cast only works for pointer , reference or pointer to datamember ?
    Consider your second example. You could have done without a cast:
    Code:
    int j = i;
    The fact that i is const does not matter since its value is copied to j.

    On the other hand, consider:
    Code:
    const int x = 123;
    const int* i = &x;
    int* j = i;
    Now this is a problem, because you could potentially write:
    Code:
    *j = 456;
    which would result in undefined behaviour since you're changing a constant. However, maybe for some reason (e.g., working with a legacy API) you really do need j to be an int*, not a const int*, but you know for sure that the value will not be changed through the pointer. In that case, it is appropriate to write:
    Code:
    int* j = const_cast<int*>(i);
    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

Tags for this Thread

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured