Re: const_cast<>

From: Rolf Magnus (ramagnus_at_t-online.de)
Date: 11/14/03


Date: Fri, 14 Nov 2003 12:56:45 +0100

R. Anbeeswaran wrote:

> Hi Mr. Ekkehard Morgenstern,
>
> Thank you very much for your answer. I understood from your answer &
> some of other references and I am concluding here:
>
> "The compiler is using the necessary calculations for the constant
> variable to perform the constant folding. So, "cout << i..." is
> calculated duing the compile time (as you told, it is from the
> compiler's constant storage.) itself. And, Since, '*p' is not constant
> variable, "...<<*p<<.." should be calculated duing the runtime."

That's more or less what happens, yes. The compiler may insert the value
of 20 directly into the code wherever you use i. But if you need a
pointer to i, it must be in memory somewhere, how else could the
address of it be taken? So real storage gets used to put the value of i
into. It might (depending on your system) be possible to change the
content of that memory location with the const_cast trick, but of
course that won't change all the assembler instructions that directly
contain the value of i. Since you told the compiler that i is const
(i.e. it will never change), it is free to do such an optimization.
The rule of thumb is that you may use const_cast _only_ if you know
exactly what you're doing. Almost every occurance of a const_cast is a
sign of a bug or design error. Either the use of the const_cast itself
is the error, or it's used to work around an error in code that cannot
be changed (e.g. a library that has a const missing somewhere).

> I'll come to my problem, where I have it. I have a class called
> "Electronics". This class can use the attributes,

Whose attributes? Electronics's ones?

> but, the scope of the maintenance of the attributes is not with in
> this class. Because, it doesn't know anything.

About what? Its own member variables?

> So, the attributes are 'Const' in "Electronics". The maintenance is
> done by someother control class called 'Service'.

Do you mean that 'Electronics' doesn't change its own member variables,
but 'Service does'? Why are they member variables of 'Electronics'
then? And why are they const if they are supposed to be changed?

> So, the result
> should be reflected from the 'Service' class into the 'Electronics'
> class. How should I go for this?

I don't understand exactly what you want. Could you give a small code
example of your idea?



Relevant Pages

  • Re: How to convert Infix notation to postfix notation
    ... and make all strings const save where the intent ... function whose contract is to change the string. ... the compiler "just" prevents the string ... try to do using the pointer you get. ...
    (comp.lang.c)
  • Re: Writing single bits to a file
    ... The 'const' keyword provides the same kind of benefit that prototypes ... enabling the compiler to warn you if it detects the fact that you ... I had not gone and more correctly fixed up the precedence heirarchy (unary ... I like to implement scripting languages for breakfast ...
    (comp.lang.c)
  • Re: const
    ... > const modifiers, ... willing to remove the const'ness with a cast, ... I don't know how good a cross-file compiler could be - it would have to be ... it's a guarantee by the programmer. ...
    (comp.programming)
  • Re: Setting PropertySheet Title (Wizard mode)
    ... If you think of it as optimization, it is probably the wrong reason. ... 'const' captures the purpose of the interface. ... I was trying to use pass-by-value to mean situations where the value ... As an old compiler writer, ...
    (microsoft.public.vc.mfc)
  • Re: Index a #define string
    ... In this case, i wouldn't use an array myself but a const char * const, but ... should one make it of static storage? ... If they were on the stack, where do you think their values ... You mean that the compiler optimises it out. ...
    (comp.lang.cpp)