const_cast operator

This is a discussion on const_cast operator within the C++ Programming forums, part of the General Programming Boards category; Code: #include<iostream> using namespace std; int main() { const int x = 10; int *p=NULL; cout<<x; p = const_cast<int*>(&x); if(p) ...

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    99

    const_cast operator

    Code:
    #include<iostream>
    using namespace std;
    
    int main()
    {
        const int x = 10;
        int *p=NULL;
        cout<<x;
        p = const_cast<int*>(&x);
        if(p)
        {
             *p = 5;
             if(p==&x)
                      cout<<"same!";            // yes both point at same loc
             cout<<x;                           //gives 10             
             cout<<*p;                          //gives 5
        }
          
        getchar();
        return 0;                    
    }
    i expect const_cast removes constness! , now points at which i have commented are the areas i have doubt on, i expect cout<<x to give 5

    However I also believe, how can i cast a const object to a non-const pointer!

  2. #2
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    Actually, this is undefined behaviour. Technically, your compiler can emit any code it wants to.

    const cast isn't meant to remove constness on a const object, it's there to remove constness on a const pointer or reference to non-const object.
    Code:
    int x = 0;
    const int y = 0;
    
    const int* cX = &x;
    const int* cy = &y;
    
    int *px = const_cast<int *>(cx);
    *px = 10; // ok, but you must KNOW that x is not const
    
    int *py = const_cast<int *>(cy);
    *py = 10; // arrghh, undefined behaviour!!
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  3. #3
    Registered User
    Join Date
    Jun 2007
    Posts
    99
    thanx !

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    And to explain the particular behaviour of your implementation: since x is a constant and thus cannot be changed, the compiler simply replaces all uses of x by its constant value.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,235
    Quote Originally Posted by ChaosEngine View Post
    const cast isn't meant to remove constness on a const object, it's there to remove constness on a const pointer or reference to non-const object.
    And to further clarify, the reason the language even allows you to do this is only because certain third party libraries are broken and take non-const parameters even when they are treated as const. const_cast<> is the only way to create a non-const pointer from a const pointer in order to pass data into these broken libraries.

    If people could be counted on to properly const-ify their input parameters, const_cast<> would have no reason to exist.

  6. #6
    Massively Single Player AverageSoftware's Avatar
    Join Date
    May 2007
    Location
    Buffalo, NY
    Posts
    141
    Quote Originally Posted by brewbuck View Post
    If people could be counted on to properly const-ify their input parameters, const_cast<> would have no reason to exist.
    Ah, but const_cast has another use. It can also be used to add or remove the volatile qualifier.
    There is no greater sign that a computing technology is worthless than the association of the word "solution" with it.

  7. #7
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,235
    Quote Originally Posted by AverageSoftware View Post
    Ah, but const_cast has another use. It can also be used to add or remove the volatile qualifier.
    True, but again, I'm not sure why you'd ever need to, if code is properly designed.

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Adding volatile is an implicit cast. It's not necessary to use const_cast for it.

    Removing it is nearly as dangerous as removing const.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  2. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Operator Overloading (Bug, or error in code?)
    By QuietWhistler in forum C++ Programming
    Replies: 2
    Last Post: 01-25-2006, 07:38 AM
  5. operator overloading and dynamic memory program
    By jlmac2001 in forum C++ Programming
    Replies: 3
    Last Post: 04-06-2003, 11:51 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21