473,396 Members | 1,655 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Casting Away Constness

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
15 7558
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
"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

"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

"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

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

"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

"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

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

"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

"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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Martin Magnusson | last post by:
I have a typedef which looks like this: typedef std::list< const S* const > seq_type; I also have a class with a private field: seq_type m_Sequence; When calling the following method
14
by: Enrico `Trippo' Porreca | last post by:
Given: typedef struct Node Node; struct Node { void *obj; Node *next; }; typedef struct Stack Stack; struct Stack {
31
by: dragoncoder | last post by:
Consider the code class A { private: int a; }; int main(void) { A x; int* ptr = (int*)&x;
7
by: dotnetchic | last post by:
Can someone explain to me the difference between static_cast<T>(v) and (what's the term for it?) old-style C cast? UINT value1 = 0; int value2 = (int)value1; int value2 =...
9
by: Alex Vinokur | last post by:
Is this approach safe? class Foo { // Stuff }; void func1 (void *) { // Do something
5
by: brekehan | last post by:
I've always been a little sketchy on the differences between static, dynamic, and reinterpret casting. I am looking to clean up the following block by using C++ casting instead of the C style...
2
by: Laurent Deniau | last post by:
I am looking for the "cleanest" way to cast away the constness of a pointee in C89, something like const_cast<T*>() in C++. Actually, I am using: #define CONST_CAST(typename,value) \ (((union {...
5
by: jason.cipriani | last post by:
There have been some recent threads about casting pointers to and from void* that have me rethinking some of my usual practices. I have a couple of questions. 1. What is the purpose of C++'s...
10
by: Alex Vinokur | last post by:
Hi, Is it possible to do C++-casting from const pair<const unsigned char*, size_t>* to const pair<unsigned char*, size_t>* ? Alex Vinokur
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.