Casting Away Constness

Discussion in 'C++' started by Trevor Lango, Jan 2, 2004.

  1. Trevor Lango

    Trevor Lango Guest

    I want to be able to cast away the constness of a private member variable in
    a member function of a class.

    I have the private data member declared as follows:

    const double x;

    I have an overloaded assignment operator implemented as follows:

    Point &Point::eek:perator=( const Point *somePoint )
    {
    *( ( double * ) &x ) = somePoint->x;
    }

    Although the above compiles, I thought the newer more acceptable way to
    accomplish this was using const_cast; however, I seem to not understand how
    to implement const_cast. I tried the following:

    const_cast< Point * >( this )->x = somePoint->x;

    But this returns the following compiler error:

    point.cpp: In method `class Point & Point::eek:perator =(const Point *)':
    point.cpp:58: assignment of read-only member `Point::x'

    Can someone please enlighten me as to the proper use of const_cast to cast
    away constness? Thanks in advance (and please excuse me if this has been
    discussed already; I just subscribed to this newsgroup and searched as far
    back as I could, but I couldn't find any mention of const_cast).
    Trevor Lango, Jan 2, 2004
    #1
    1. Advertising

  2. Trevor Lango

    Jeff Schwab Guest

    Trevor Lango wrote:
    > I want to be able to cast away the constness of a private member variable in
    > a member function of a class.


    Are you familiar with the "mutable" keword?

    > I have the private data member declared as follows:
    >
    > const double x;


    Why did you declare it const, if you knew you were going to change it?

    > I have an overloaded assignment operator implemented as follows:
    >
    > Point &Point::eek:perator=( const Point *somePoint )
    > {
    > *( ( double * ) &x ) = somePoint->x;
    > }
    >
    > Although the above compiles, I thought the newer more acceptable way to
    > accomplish this was using const_cast; however, I seem to not understand how
    > to implement const_cast. I tried the following:
    >
    > const_cast< Point * >( this )->x = somePoint->x;
    >
    > But this returns the following compiler error:
    >
    > point.cpp: In method `class Point & Point::eek:perator =(const Point *)':
    > point.cpp:58: assignment of read-only member `Point::x'


    You "unconsted" the Point object. The member "x" was const for a
    different reason, namely because you declared it thus.

    > Can someone please enlighten me as to the proper use of const_cast to cast
    > away constness?


    const_cast< double >( x ) = somePoint->x;

    If you find yourself needing const cast, there is an inconsistency
    somewhere in your program. If you understand what the inconsistency is,
    and you're OK with it, you still might consider a mutable member as an
    alternative to the cast.

    > Thanks in advance (and please excuse me if this has been
    > discussed already; I just subscribed to this newsgroup and searched as far
    > back as I could, but I couldn't find any mention of const_cast).


    Welcome!

    -Jeff
    Jeff Schwab, Jan 2, 2004
    #2
    1. Advertising

  3. Trevor Lango

    Jeff Schwab Guest

    Jeff Schwab wrote:
    > Trevor Lango wrote:
    >
    >> I want to be able to cast away the constness of a private member
    >> variable in a member function of a class.

    >
    >
    > Are you familiar with the "mutable" keyword?
    >
    >> I have the private data member declared as follows:
    >>
    >> const double x;

    >
    > Why did you declare it const, if you knew you were going to change it?
    >
    >> I have an overloaded assignment operator implemented as follows:
    >>
    >> Point &Point::eek:perator=( const Point *somePoint )
    >> {
    >> *( ( double * ) &x ) = somePoint->x;
    >> }
    >>
    >> Although the above compiles, I thought the newer more acceptable way to
    >> accomplish this was using const_cast; however, I seem to not
    >> understand how
    >> to implement const_cast. I tried the following:
    >>
    >> const_cast< Point * >( this )->x = somePoint->x;
    >>
    >> But this returns the following compiler error:
    >>
    >> point.cpp: In method `class Point & Point::eek:perator =(const Point
    >> *)':
    >> point.cpp:58: assignment of read-only member `Point::x'

    >
    >
    > You "unconsted" the Point object. The member "x" was const for a
    > different reason, namely because you declared it thus.
    >
    >> Can someone please enlighten me as to the proper use of const_cast to
    >> cast
    >> away constness?

    >
    >
    > const_cast< double >( x ) = somePoint->x;


    Sorry, that should say:

    const_cast< double >( x ) = somePoint->x;

    Btw, const_cast is not guaranteed to work. You may get a silent error
    at run time. Try making the member mutable, or at least not declaring
    it const.

    > If you find yourself needing const cast, there is an inconsistency
    > somewhere in your program. If you understand what the inconsistency is,
    > and you're OK with it, you still might consider a mutable member as an
    > alternative to the cast.
    >
    >> Thanks in advance (and please excuse me if this has been
    >> discussed already; I just subscribed to this newsgroup and searched as
    >> far
    >> back as I could, but I couldn't find any mention of const_cast).

    >
    >
    > Welcome!
    >
    > -Jeff
    >
    Jeff Schwab, Jan 2, 2004
    #3
  4. "Trevor Lango" <> wrote...
    > I want to be able to cast away the constness of a private member variable

    in
    > a member function of a class.
    >
    > I have the private data member declared as follows:
    >
    > const double x;


    Why const?

    >
    > I have an overloaded assignment operator implemented as follows:
    >
    > Point &Point::eek:perator=( const Point *somePoint )


    Why are you assigning from a pointer? One rather strange assignment op.
    Well, no matter...

    > {
    > *( ( double * ) &x ) = somePoint->x;


    Simpler would be

    (double&) x = somePoint->x;

    > }
    >
    > Although the above compiles, I thought the newer more acceptable way to
    > accomplish this was using const_cast; however, I seem to not understand

    how
    > to implement const_cast. I tried the following:
    >
    > const_cast< Point * >( this )->x = somePoint->x;
    >
    > But this returns the following compiler error:
    >
    > point.cpp: In method `class Point & Point::eek:perator =(const Point *)':
    > point.cpp:58: assignment of read-only member `Point::x'


    You cast away constness of the object itself, which is (a) unneeded because
    the object is already non-const and (b) doesn't affect the const-ness of
    a member declared explicitly const.

    You _could_ do

    const_cast<double&>(x) = somePoint->x;

    But still, you have to answer this question: why did you make the member
    'const' in the first place?

    >
    > Can someone please enlighten me as to the proper use of const_cast to cast
    > away constness?


    I think you need some enlightment as to proper use of const before you
    attempt using const_cast... But that's just the impression I get.

    > Thanks in advance (and please excuse me if this has been
    > discussed already; I just subscribed to this newsgroup and searched as far
    > back as I could, but I couldn't find any mention of const_cast).


    Casting away const-ness is not what you should be doing casually. In
    most cases if you cast away const-ness, you _must_ be sure that the
    original object _was_ declared/defined as non-const, otherwise UB occurs.

    Victor
    Victor Bazarov, Jan 2, 2004
    #4
  5. Trevor Lango

    Jeff Schwab Guest

    Ron Natalie wrote:
    > "Jeff Schwab" <> wrote in message news:...
    >
    >>Trevor Lango wrote:
    >>
    >>>I want to be able to cast away the constness of a private member variable in
    >>>a member function of a class.

    >>
    >>Are you familiar with the "mutable" keword?

    >
    >
    > He might be, but it wouldn't help. He has a const member in a non-const object
    > he wishes to change. Mutable would allow him to change a member in a CONST
    > object.


    Yes, not declaring the member "const" in the first place certainly would
    help. :)
    Jeff Schwab, Jan 2, 2004
    #5
  6. Trevor Lango

    Ron Natalie Guest

    "Trevor Lango" <> wrote in message news:6vlJb.4740$...
    > I want to be able to cast away the constness of a private member variable in
    > const_cast< Point * >( this )->x = somePoint->x;

    *const_cast<Point*>(&x) = somePoint->x;
    or
    const_cast<Point&>(x) = somePoint->x;
    Ron Natalie, Jan 2, 2004
    #6
  7. Trevor Lango

    Ron Natalie Guest

    "Trevor Lango" <> wrote in message news:6vlJb.4740$...
    > I want to be able to cast away the constness of a private member variable in
    > a member function of a class.
    >
    > I have the private data member declared as follows:
    >
    > const double x;
    >
    > I have an overloaded assignment operator implemented as follows:
    >
    > Point &Point::eek:perator=( const Point *somePoint )
    > {
    > *( ( double * ) &x ) = somePoint->x;
    > }
    >

    Oops, I got confused before:
    *const_cast<double*>(&x) = somePoint->x;
    or
    const_cast<double&>(x) = somePoint->x
    Ron Natalie, Jan 2, 2004
    #7
  8. Trevor Lango

    Ron Natalie Guest

    "Jeff Schwab" <> wrote in message news:...
    > Trevor Lango wrote:
    > > I want to be able to cast away the constness of a private member variable in
    > > a member function of a class.

    >
    > Are you familiar with the "mutable" keword?


    He might be, but it wouldn't help. He has a const member in a non-const object
    he wishes to change. Mutable would allow him to change a member in a CONST
    object.
    Ron Natalie, Jan 2, 2004
    #8
  9. Trevor Lango

    Trevor Lango Guest

    "Jeff Schwab" <> wrote in message
    news:...
    > Trevor Lango wrote:
    > > I want to be able to cast away the constness of a private member

    variable in
    > > a member function of a class.

    >
    > Are you familiar with the "mutable" keword?


    I thought the mutable keyword was for allowing modification of private
    member variables in class objects declared as const...?

    > > I have the private data member declared as follows:
    > >
    > > const double x;

    >
    > Why did you declare it const, if you knew you were going to change it?


    I didn't want anything to be able to modify it except for the overloaded
    assignment operator.

    [snipped for readability]
    Trevor Lango, Jan 2, 2004
    #9
  10. Trevor Lango

    Jeff Schwab Guest

    Trevor Lango wrote:
    > "Jeff Schwab" <> wrote in message
    > news:...
    >
    >>Trevor Lango wrote:
    >>
    >>>I want to be able to cast away the constness of a private member

    >
    > variable in
    >
    >>>a member function of a class.

    >>
    >>Are you familiar with the "mutable" keword?

    >
    >
    > I thought the mutable keyword was for allowing modification of private
    > member variables in class objects declared as const...?


    Exactly. You were casting away the constness of the object just long
    enough to modify a member variable. That's exactly the sort of cast
    "mutable" was meant to help you avoid.

    >>>I have the private data member declared as follows:
    >>>
    >>> const double x;

    >>
    >>Why did you declare it const, if you knew you were going to change it?

    >
    >
    > I didn't want anything to be able to modify it except for the overloaded
    > assignment operator.


    Hmmm... I'm not sure of a way to make that sort of guarantee, although
    I do see what you mean.

    >
    > [snipped for readability]
    >
    >
    Jeff Schwab, Jan 2, 2004
    #10
  11. Trevor Lango

    Trevor Lango Guest

    "Jeff Schwab" <> wrote in message
    news:...
    > Ron Natalie wrote:
    > > "Jeff Schwab" <> wrote in message

    news:...
    > >
    > >>Trevor Lango wrote:
    > >>
    > >>>I want to be able to cast away the constness of a private member

    variable in
    > >>>a member function of a class.
    > >>
    > >>Are you familiar with the "mutable" keword?

    > >
    > >
    > > He might be, but it wouldn't help. He has a const member in a

    non-const object
    > > he wishes to change. Mutable would allow him to change a member in a

    CONST
    > > object.

    >
    > Yes, not declaring the member "const" in the first place certainly would
    > help. :)
    >


    After some further thought I do not believe I will implement those private
    member variables as const. However, I do appreciate the clarification of the
    proper syntax for implementing const_cast. For those of you who posted the
    appropriate syntax, can you please cite what documentation you referenced to
    provide your answers? Thanks!
    Trevor Lango, Jan 2, 2004
    #11
  12. Trevor Lango

    Jeff Schwab Guest

    Ron Natalie wrote:
    > "Jeff Schwab" <> wrote in message news:...
    >
    >>>>const_cast< double >( x ) = somePoint->x;

    >>
    >>Sorry, that should say:
    >>
    >> const_cast< double >( x ) = somePoint->x;

    >
    > That loks the same to me, you mean
    > const_cast<double&>(x) = somePoint->x;


    Right, thank you.

    >
    >
    >>Btw, const_cast is not guaranteed to work. You may get a silent error
    >>at run time. Try making the member mutable, or at least not declaring
    >>it const.

    >
    >
    > There's no point in making it mutable. The member function he is running
    > is not CONST, hence the member is wouldn't be const if he didn't explicitly
    > make it so.


    Since the OP has explained further, I agree. He wasn't actually casting
    away the constness of the variable though, he was casting away the
    constness of the object. I was offering mutable as a suggestion to
    achieve a similar effect, much as you suggested this:

    const_cast<Point&>(x) = somePoint->x;
    Jeff Schwab, Jan 2, 2004
    #12
  13. Trevor Lango

    Ron Natalie Guest

    "Jeff Schwab" <> wrote in message news:...
    >> > const_cast< double >( x ) = somePoint->x;

    >
    > Sorry, that should say:
    >
    > const_cast< double >( x ) = somePoint->x;

    That loks the same to me, you mean
    const_cast<double&>(x) = somePoint->x;

    >
    > Btw, const_cast is not guaranteed to work. You may get a silent error
    > at run time. Try making the member mutable, or at least not declaring
    > it const.


    There's no point in making it mutable. The member function he is running
    is not CONST, hence the member is wouldn't be const if he didn't explicitly
    make it so.
    Ron Natalie, Jan 2, 2004
    #13
  14. Trevor Lango

    Ron Natalie Guest

    "Jeff Schwab" <> wrote in message news:...
    > >
    > > I thought the mutable keyword was for allowing modification of private
    > > member variables in class objects declared as const...?

    >
    > Exactly. You were casting away the constness of the object just long
    > enough to modify a member variable. That's exactly the sort of cast
    > "mutable" was meant to help you avoid.


    The object wasn't const. The member was.
    Ron Natalie, Jan 2, 2004
    #14
  15. Trevor Lango

    Ron Natalie Guest

    "Trevor Lango" <> wrote in message news:LYlJb.4745$...
    > After some further thought I do not believe I will implement those private
    > member variables as const. However, I do appreciate the clarification of the
    > proper syntax for implementing const_cast. For those of you who posted the
    > appropriate syntax, can you please cite what documentation you referenced to
    > provide your answers? Thanks!
    >

    I didn't have to reference anything. If you spend several years actually writing
    code you know these things :)

    5.2.11 of the standard pretty nicely explains this (and points you to the section
    that warns you that what you're doing is undefined).
    Ron Natalie, Jan 2, 2004
    #15
  16. Trevor Lango

    Ron Natalie Guest

    "Jeff Schwab" <> wrote in message news:...
    > Since the OP has explained further, I agree. He wasn't actually casting
    > away the constness of the variable though, he was casting away the
    > constness of the object. I was offering mutable as a suggestion to
    > achieve a similar effect, much as you suggested this:
    >
    > const_cast<Point&>(x) = somePoint->x;
    >

    Which was wrong. I meant double above. The Point object wasn't
    const (as it was in a non-const method).
    Ron Natalie, Jan 2, 2004
    #16
    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. Martin Magnusson

    Casting away constness

    Martin Magnusson, Nov 17, 2003, in forum: C++
    Replies:
    1
    Views:
    384
    tom_usenet
    Nov 17, 2003
  2. Replies:
    14
    Views:
    827
    Ian Collins
    Apr 4, 2006
  3. Laurent Deniau

    casting away constness

    Laurent Deniau, Sep 12, 2007, in forum: C Programming
    Replies:
    3
    Views:
    2,009
    =?iso-2022-kr?q?=1B=24=29CHarald_van_D=0E=29=26=0F
    Sep 12, 2007
  4. copx

    casting const away

    copx, Jan 6, 2011, in forum: C Programming
    Replies:
    58
    Views:
    1,360
    Michael Press
    Feb 10, 2011
  5. Ben Bacarisse

    Re: Few questions about casting away const.

    Ben Bacarisse, Mar 4, 2014, in forum: C Programming
    Replies:
    0
    Views:
    70
    Ben Bacarisse
    Mar 4, 2014
Loading...

Share This Page