Re: const_cast<>
From: Rolf Magnus (ramagnus_at_t-online.de)
Date: 11/14/03
- Next message: Rolf Magnus: "Re: Isn't 'vector' a misnomer?"
- Previous message: kanze_at_gabi-soft.fr: "Re: thisIsAMemberFunctionName vs this_is_a_member_function_name"
- In reply to: R. Anbeeswaran: "Re: const_cast<>"
- Next in thread: Ekkehard Morgenstern: "Re: const_cast<>"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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?
- Next message: Rolf Magnus: "Re: Isn't 'vector' a misnomer?"
- Previous message: kanze_at_gabi-soft.fr: "Re: thisIsAMemberFunctionName vs this_is_a_member_function_name"
- In reply to: R. Anbeeswaran: "Re: const_cast<>"
- Next in thread: Ekkehard Morgenstern: "Re: const_cast<>"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|