const keyword

R

Ravi

I have my TC++ help file which has the following in it:

const my_age = 39;
___________________________
WARNING: A const variable can be indirectly modified by a
pointer, as in
the following:

*(int *)&my_age = 35;

When the const modifier is used with a pointer parameter in
a function's
parameter list, the function cannot modify the variable
that the pointer
points to. For example,

int printf (const char *format, ...);

Here the printf function is prevented from modifying the
format string.
_________________

I can't figure out what they are trying to say.
Could someone help?

TIA.
 
J

Josh Sebastian

I have my TC++ help file which has the following in it:

const my_age = 39;
___________________________
WARNING: A const variable can be indirectly modified by a
pointer, as in
the following:

*(int *)&my_age = 35;

They're saying that you can cast away const. This might or might not work
all the time (it'll compile, but it might fail in strange ways at
runtime). If your compiler documentation says you can do it, though, it
should be safe enough with that compiler.
When the const modifier is used with a pointer parameter in
a function's
parameter list, the function cannot modify the variable
that the pointer
points to. For example,

int printf (const char *format, ...);

Here the printf function is prevented from modifying the
format string.

They're saying that the string (the object format points to) is what's
const, not format itself.

int const* p; // *p is const
int* const p; // p is const

Josh
 
R

Rolf Magnus

osmium said:
With my compiler: no errors, no warnings, no work. The const was not
changed.

That's the reason why you should avoid C style casts. You tell the
compiler to be silent and do whatever it takes to convert to the
specified type. You basically say "shut up, I know this doesn't make
sense, but I want it anyway coz I know what I'm doing". Of course you
_must_ actually know what you're doing.
 
O

osmium

Josh said:
They're saying that you can cast away const. This might or might not work
all the time (it'll compile, but it might fail in strange ways at
runtime). If your compiler documentation says you can do it, though, it
should be safe enough with that compiler.

With my compiler: no errors, no warnings, no work. The const was not
changed.

Borland TC++W 3.1
 
M

Micah Cowan

Perhaps the documentation is outdated; or perhaps they merely
wanted to point out that it *might* work when executed on other
platforms (of course, it might induce nasal daemons to fly out
your nose, too ;-) )
That's the reason why you should avoid C style casts. You tell the
compiler to be silent and do whatever it takes to convert to the
specified type. You basically say "shut up, I know this doesn't make
sense, but I want it anyway coz I know what I'm doing". Of course you
_must_ actually know what you're doing.

How would that be any less true using const_cast<> ?

-Micah
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top