Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Casting Away Constness

Reply
Thread Tools

Casting Away Constness

 
 
Trevor Lango
Guest
Posts: n/a
 
      01-02-2004
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: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:perator =(const Point *)':
point.cpp:58: assignment of read-only member `Point:'

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).


 
Reply With Quote
 
 
 
 
Jeff Schwab
Guest
Posts: n/a
 
      01-02-2004
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: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:perator =(const Point *)':
> point.cpp:58: assignment of read-only member `Point:'


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

 
Reply With Quote
 
 
 
 
Jeff Schwab
Guest
Posts: n/a
 
      01-02-2004
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: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:perator =(const Point
>> *)':
>> point.cpp:58: assignment of read-only member `Point:'

>
>
> 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
>


 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      01-02-2004
"Trevor Lango" <(E-Mail Removed)> 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: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:perator =(const Point *)':
> point.cpp:58: assignment of read-only member `Point:'


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


 
Reply With Quote
 
Jeff Schwab
Guest
Posts: n/a
 
      01-02-2004
Ron Natalie wrote:
> "Jeff Schwab" <(E-Mail Removed)> wrote in message news:(E-Mail Removed)...
>
>>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.

 
Reply With Quote
 
Ron Natalie
Guest
Posts: n/a
 
      01-02-2004

"Trevor Lango" <(E-Mail Removed)> wrote in message news:6vlJb.4740$(E-Mail Removed) m...
> 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;

 
Reply With Quote
 
Ron Natalie
Guest
Posts: n/a
 
      01-02-2004

"Trevor Lango" <(E-Mail Removed)> wrote in message news:6vlJb.4740$(E-Mail Removed) m...
> 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: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

 
Reply With Quote
 
Ron Natalie
Guest
Posts: n/a
 
      01-02-2004

"Jeff Schwab" <(E-Mail Removed)> wrote in message news:(E-Mail Removed)...
> 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.

 
Reply With Quote
 
Trevor Lango
Guest
Posts: n/a
 
      01-02-2004

"Jeff Schwab" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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]


 
Reply With Quote
 
Jeff Schwab
Guest
Posts: n/a
 
      01-02-2004
Trevor Lango wrote:
> "Jeff Schwab" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>
>>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]
>
>


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
casting const away copx C Programming 58 02-10-2011 02:24 AM
casting away constness Laurent Deniau C Programming 3 09-12-2007 05:26 PM
Constness of the container of shared_ptr and the constness of it elements PengYu.UT@gmail.com C++ 14 04-04-2006 01:57 AM
Another question about inheritance (up-casting and down-casting) kevin Java 11 01-08-2005 07:11 PM
Casting away constness Martin Magnusson C++ 1 11-17-2003 01:30 PM



Advertisments