By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,172 Members | 1,129 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,172 IT Pros & Developers. It's quick & easy.

Casting Away Constness

P: n/a
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::operator=( 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::operator =(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).
Jul 22 '05 #1
Share this Question
Share on Google+
15 Replies


P: n/a
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::operator=( 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::operator =(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

Jul 22 '05 #2

P: n/a
"Trevor Lango" <tm*****@sbcglobal.net> 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::operator=( 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::operator =(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
Jul 22 '05 #3

P: n/a

"Trevor Lango" <tm*****@sbcglobal.net> wrote in message news:6v*****************@newssvr27.news.prodigy.co 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;

Jul 22 '05 #4

P: n/a

"Trevor Lango" <tm*****@sbcglobal.net> wrote in message news:6v*****************@newssvr27.news.prodigy.co 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::operator=( 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

Jul 22 '05 #5

P: n/a

"Jeff Schwab" <je******@comcast.net> wrote in message news:0Y********************@comcast.com...
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.

Jul 22 '05 #6

P: n/a
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::operator=( 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::operator =(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


Jul 22 '05 #7

P: n/a
Ron Natalie wrote:
"Jeff Schwab" <je******@comcast.net> wrote in message news:0Y********************@comcast.com...
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. :)

Jul 22 '05 #8

P: n/a

"Jeff Schwab" <je******@comcast.net> wrote in message
news:0Y********************@comcast.com...
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]
Jul 22 '05 #9

P: n/a

"Jeff Schwab" <je******@comcast.net> wrote in message
news:8r********************@comcast.com...
Ron Natalie wrote:
"Jeff Schwab" <je******@comcast.net> wrote in message news:0Y********************@comcast.com...
Trevor Lango wrote:

I want to be able to cast away the constness of a private member variable ina 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!
Jul 22 '05 #10

P: n/a

"Jeff Schwab" <je******@comcast.net> wrote in message news:MY********************@comcast.com...
> 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.

Jul 22 '05 #11

P: n/a
Trevor Lango wrote:
"Jeff Schwab" <je******@comcast.net> wrote in message
news:0Y********************@comcast.com...
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]


Jul 22 '05 #12

P: n/a
Ron Natalie wrote:
"Jeff Schwab" <je******@comcast.net> wrote in message news:MY********************@comcast.com...
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;
Jul 22 '05 #13

P: n/a

"Jeff Schwab" <je******@comcast.net> wrote in message news:_L********************@comcast.com...

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.

Jul 22 '05 #14

P: n/a

"Trevor Lango" <tm*****@sbcglobal.net> wrote in message news:LY****************@newssvr27.news.prodigy.com ...
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).

Jul 22 '05 #15

P: n/a

"Jeff Schwab" <je******@comcast.net> wrote in message news:_t********************@comcast.com...
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).

Jul 22 '05 #16

This discussion thread is closed

Replies have been disabled for this discussion.