basic question on const_cast

Discussion in 'C++' started by pauldepstein@att.net, Feb 1, 2008.

  1. Guest

    Textbooks usually say that const_cast can "add or remove" a const
    modifier. However, all the example I've seen are examples of
    "remove". I've never seen a const_cast used to make something const
    that was originally non_const. Is this ever done?

    Thanks,

    Paul Epstein
     
    , Feb 1, 2008
    #1
    1. Advertising

  2. Salt_Peter Guest

    On Jan 31, 9:34 pm, wrote:
    > Textbooks usually say that const_cast can "add or remove" a const
    > modifier. However, all the example I've seen are examples of
    > "remove". I've never seen a const_cast used to make something const
    > that was originally non_const. Is this ever done?
    >
    > Thanks,
    >
    > Paul Epstein


    yes, everywhere, except it doesn't take the form you are thinking of.
    as an example:

    void foo(const int i)
    {
    // i = 88; // error
    // read/access the const var
    }

    int main()
    {
    int n(99);
    foo( n );
    n = 88; // ok
    }

    same goes for say... a copy constructor

    class A
    {
    public:
    A(const A& copy) { }
    };
     
    Salt_Peter, Feb 1, 2008
    #2
    1. Advertising

  3. Sean Hunt Guest

    On Jan 31, 7:34 pm, wrote:
    > Textbooks usually say that const_cast can "add or remove" a const
    > modifier. However, all the example I've seen are examples of
    > "remove". I've never seen a const_cast used to make something const
    > that was originally non_const. Is this ever done?
    >
    > Thanks,
    >
    > Paul Epstein


    Usually, the ability to remove const_cast is done to work around bad
    design - the inverse is also true. Consider the following class:

    class A
    {
    int i;
    public:
    int operator () (int i2) { return i = i2; }
    int operator () (int i2) const { return i; }
    };

    In this case, you might have to const_cast to force the overload to
    the const version (because bad design means it acts differently).
     
    Sean Hunt, Feb 1, 2008
    #3
  4. * Salt_Peter:
    > On Jan 31, 9:34 pm, wrote:
    >> Textbooks usually say that const_cast can "add or remove" a const
    >> modifier. However, all the example I've seen are examples of
    >> "remove". I've never seen a const_cast used to make something const
    >> that was originally non_const. Is this ever done?
    >>
    >> Thanks,
    >>
    >> Paul Epstein

    >
    > yes, everywhere, except it doesn't take the form you are thinking of.
    > as an example:
    >
    > void foo(const int i)
    > {
    > // i = 88; // error
    > // read/access the const var
    > }
    >
    > int main()
    > {
    > int n(99);
    > foo( n );
    > n = 88; // ok
    > }
    >
    > same goes for say... a copy constructor
    >
    > class A
    > {
    > public:
    > A(const A& copy) { }
    > };


    Huh?


    Cheers, bewildered,

    - Alf

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
     
    Alf P. Steinbach, Feb 1, 2008
    #4
  5. * :
    > Textbooks usually say that const_cast can "add or remove" a const
    > modifier. However, all the example I've seen are examples of
    > "remove". I've never seen a const_cast used to make something const
    > that was originally non_const. Is this ever done?


    Yes, e.g. in the case where you want to call a const member function
    overload, but it is probably better expressed using a reference.

    I.e., instead of

    const_cast<T const*>( this )->foo(); // Calling const overload.

    it might be preferable to do

    T const* const constSelf = this;
    constSelf->foo();

    With that convention, searching for "const_cast" in the code will yield
    only casts that cast away constness.


    Cheers, & hth.,

    - Alf

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
     
    Alf P. Steinbach, Feb 1, 2008
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kaspar Minosiants

    [help] const_cast

    Kaspar Minosiants, Jul 21, 2003, in forum: C++
    Replies:
    2
    Views:
    447
    John Harrison
    Jul 21, 2003
  2. drowned

    const_cast question

    drowned, Aug 4, 2003, in forum: C++
    Replies:
    3
    Views:
    514
    Josephine Schafer
    Aug 4, 2003
  3. R. Anbeeswaran

    const_cast<>

    R. Anbeeswaran, Nov 13, 2003, in forum: C++
    Replies:
    7
    Views:
    640
    Ekkehard Morgenstern
    Nov 14, 2003
  4. Gajanan Bhat

    const_cast question

    Gajanan Bhat, May 4, 2004, in forum: C++
    Replies:
    9
    Views:
    574
    Bill Seurer
    May 5, 2004
  5. , India
    Replies:
    4
    Views:
    545
    red floyd
    Nov 14, 2007
Loading...

Share This Page