how to make a non const copy of a const variable?

This is a discussion on how to make a non const copy of a const variable? within the C++ Programming forums, part of the General Programming Boards category; Code: const int a = 10; int b; b = const_cast <int> (a); I wonder why this isn`t working. How ...

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    88

    how to make a non const copy of a const variable?

    Code:
    const int a = 10;
    int b;
    b = const_cast <int> (a);
    I wonder why this isn`t working.

    How to make a simple non const copy of a const variable?

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Code:
    int b = a;
    You can't change consts. But all it takes to make a copy is reading the value, and of course you can do that.
    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

  3. #3
    Registered User
    Join Date
    Jul 2007
    Posts
    88
    No way?

    I could write it to a file and then read it... But... You know....

    No more elegant way?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by sept View Post
    No way?

    I could write it to a file and then read it... But... You know....

    No more elegant way?
    Who said anything about a file? You just need to copy it (read the value into a new variable). In other words, take out the const_cast and see what that does for you.

  5. #5
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,627
    He means that you can't change consts.

    About the only thing the const keyword does is prevent code from compiling when a const is used as an l-value.

    Basically this means:
    Code:
    const int a = 10;
    // a = 20; won't compile
    int b = a; // OK
    Not very complicated unfortunately.

  6. #6
    Registered User
    Join Date
    Jul 2007
    Posts
    88
    My memory was that
    Code:
    const int a = 5;
    int b = a;
    brings a compile error can not convert from const int to int. But somehow I really messed up.
    Solved, Thanks.

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,422
    Const just means you can't change the original memory, but since you're making a copy of the given value at memory, it works fine.
    Problem comes when passing pointers around. Since they all point to the original memory, you can't pass a const pointer where a normal pointer is expected.
    Though it is possible to do away with const for such things as pointers. But they do put the implementation at risk, so do it with care.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. seg fault at vectornew
    By tytelizgal in forum C Programming
    Replies: 2
    Last Post: 10-25-2008, 01:22 PM
  2. Need some help...
    By darkconvoy in forum C Programming
    Replies: 32
    Last Post: 04-29-2008, 03:33 PM
  3. fatal error LNK1104
    By DMH in forum C++ Programming
    Replies: 2
    Last Post: 11-16-2005, 02:46 AM
  4. How do i un-SHA1 hash something..
    By willc0de4food in forum C Programming
    Replies: 4
    Last Post: 09-14-2005, 05:59 AM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM

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