Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Can I modify a constant variable through a pointer obtained from const_cast?

Reply
Thread Tools

Can I modify a constant variable through a pointer obtained from const_cast?

 
 
JKop
Guest
Posts: n/a
 
      07-22-2004

It seems to me that C++ makes too many allowances for C's
faults. For instance:

char* blah = "JKop";
blah[2] = 't';

Undefined behaviour. But we wouldn't have this problem in
the first place if that didn't compile.

The next "version" of C++ should be "C++ without the C
bullshit".

-JKop
 
Reply With Quote
 
 
 
 
Andrew Koenig
Guest
Posts: n/a
 
      07-22-2004
"Siemel Naran" <(E-Mail Removed)> wrote in message
news:LgRLc.123119$(E-Mail Removed)...

> > > const int i = 0;


> True, but if you take the address of the variable and use this address,

the
> compiler must allocate memory for it.


> > > int &j = const_cast<int&>(i);


No, because this use of const_cast yields undefined behavior, which means
that the compiler is permitted to do as it pleases.


 
Reply With Quote
 
 
 
 
Andrew Koenig
Guest
Posts: n/a
 
      07-22-2004
"JKop" <(E-Mail Removed)> wrote in message
news:V9SLc.5268$(E-Mail Removed)...

> Can some-one please explain to me why "const_cast" exists
> at all? From what I can see it just produces undefined
> behaviour.


Here is an example of a use of const_cast that is well defined:

void f()
{
int x;
const int* p = &x;
int* q = const_cast<int*>(p);
*q = 42;
}



 
Reply With Quote
 
Andre Kostur
Guest
Posts: n/a
 
      07-22-2004
JKop <(E-Mail Removed)> wrote in news:8OTLc.5288$(E-Mail Removed):

>
> It seems to me that C++ makes too many allowances for C's
> faults. For instance:
>
> char* blah = "JKop";
> blah[2] = 't';
>
> Undefined behaviour. But we wouldn't have this problem in
> the first place if that didn't compile.
>
> The next "version" of C++ should be "C++ without the C
> bullshit".


You're in the wrong place for that argument. You want to be over in
comp.std.c++
 
Reply With Quote
 
Siemel Naran
Guest
Posts: n/a
 
      07-23-2004
"Ali Cehreli" <(E-Mail Removed)> wrote in message
> On Thu, 22 Jul 2004 09:43:09 -0700, JKop wrote:
> > Siemel Naran posted:


> >> const_cast is only garaunteed to work when the original object was
> >> const.


Above "const" should be "non-const". Sorry for typo.


 
Reply With Quote
 
Siemel Naran
Guest
Posts: n/a
 
      07-23-2004
"Andre Kostur" <(E-Mail Removed)> wrote in message

> You may be attempting to call legacy code which was not const-correct.
> Let's assume that you have an old C library that has a function:
>
> void fn(data * pdata);
>
> And it is well-documented that fn() does not modify the data.
>
> Now, in your C++ code you have:
>
> void cfn(const data & somedata)
> {
> fn(&somedata);
> }
>
>
> OK, you _tried_ to have that, but the C++ compiler rejects it since you
> are attempting to pass a const data* where the function signature is only
> data*. However, this is legal and safe:
>
> void cfn(const data & somedata)
> {
> fn(const_cast<data *>(&somedata));
> }
>
> Since fn is documented to not change the passed in data, this is safe and
> legal.


This is a good example. Another uage is to implement the non-const function
in terms of the const one.

const X::value_type& X::get(int i) const {
// lots of code
};

inline
X::value_type& X::get(int i) {
const X * cthis = this;
return const_cast<value_type&>(cthis->get());
};


 
Reply With Quote
 
JKop
Guest
Posts: n/a
 
      07-23-2004
Siemel Naran posted:

>> Since fn is documented to not change the passed in data,

this is safe
>> and legal.

>
> This is a good example. Another uage is to implement the

non-const
> function in terms of the const one.
>
> const X::value_type& X::get(int i) const {
> // lots of code
> };
>
> inline
> X::value_type& X::get(int i) {
> const X * cthis = this;
> return const_cast<value_type&>(cthis->get());
> };



Since these functions will have extern "C" in front of
them, the linker won't know anything about the arguments to
it, whether or not they'll be const. Why doesn't one just
change the header file and stick in "const" where
appropriate?

-JKop
 
Reply With Quote
 
Karl Heinz Buchegger
Guest
Posts: n/a
 
      07-23-2004
JKop wrote:
>
> Siemel Naran posted:
>
> >> Since fn is documented to not change the passed in data,

> this is safe
> >> and legal.

> >
> > This is a good example. Another uage is to implement the

> non-const
> > function in terms of the const one.
> >
> > const X::value_type& X::get(int i) const {
> > // lots of code
> > };
> >
> > inline
> > X::value_type& X::get(int i) {
> > const X * cthis = this;
> > return const_cast<value_type&>(cthis->get());
> > };

>
> Since these functions will have extern "C" in front of
> them, the linker won't know anything about the arguments to
> it, whether or not they'll be const. Why doesn't one just
> change the header file and stick in "const" where
> appropriate?


If it's one of your own headers, then this is surely the way to go.
But often you don't have that luxary. Eg. You bought a library
and the header came with that library. Sure, you can change the header
but this gets tedious very fast, when the next update of that library
arives.


--
Karl Heinz Buchegger
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
Siemel Naran
Guest
Posts: n/a
 
      07-23-2004
"JKop" <(E-Mail Removed)> wrote in message news%5Mc.5337
> Siemel Naran posted:


> > This is a good example. Another uage is to implement the

> non-const
> > function in terms of the const one.
> >
> > const X::value_type& X::get(int i) const {
> > // lots of code
> > };
> >
> > inline
> > X::value_type& X::get(int i) {
> > const X * cthis = this;
> > return const_cast<value_type&>(cthis->get());
> > };

>
>
> Since these functions will have extern "C" in front of
> them, the linker won't know anything about the arguments to
> it, whether or not they'll be const. Why doesn't one just
> change the header file and stick in "const" where
> appropriate?


My example is valid C++, where you implement both a const and non-const
function, like vector<T>:perator[](size_type) comes in both flavors.
Extern has nothing to do with it.


 
Reply With Quote
 
Siemel Naran
Guest
Posts: n/a
 
      07-26-2004
"JKop" <(E-Mail Removed)> wrote in message news:V9SLc.5268

> Can some-one please explain to me why "const_cast" exists
> at all? From what I can see it just produces undefined
> behaviour.


Another use of const_cast, which seems ok to me, is in my matrix code.

There is a class matrix<T>

It has const and non-const functions to get a row
typename matrix<T>::row_t matrix<T>::row(int i);
typename matrix<T>::const_row_t matrix<T>::row(int i) const;

The nested class matrix<T>::row_t is public, but its constructor is private,
and only the matrix<T>::row(int) functions can create instances of row_t and
const_row_t objects.

There is a function to swap two rows. Ordinarily, this function would be
defined as
void matrix<T>::swap(row_t&, row_t&);
The swap function changes both the matrix as well as the function arguments
by making them point to the new rows.

But because I want the user to be able to say
m.swap(m.row(1), m.row(2));

the swap function is
void matrix<T>::swap(const row_t&, const row_t&);

In the body of the swap function I cast away const of the function
arguments. This is OK because the original row_t object, created in
matrix<T>::row(int), was created as not const.


 
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
Can I include include a constant in a constant array? Fredxx VHDL 3 07-15-2009 07:39 AM
Can't modify constant item in scalar assignment emrefan Perl Misc 4 06-05-2007 03:21 AM
I 'VE JUST OBTAINED MY MCSD FRANCIS KAITANO MCSD 2 06-08-2005 03:32 PM
"Non-constant" constant can't be used as template argument Martin Magnusson C++ 2 10-08-2004 08:41 AM
pointer to member function and pointer to constant member function Fraser Ross C++ 4 08-14-2004 06:00 PM



Advertisments