How can I const cast a reference

P

Plissken.s

Is it possible to const_cast a reference?

If not , how can I remove to 'const' from a reference?

void aMethod2(A& a) {

}


void aMethod1(const A& a) {
// how can I call aMethod2
}

Thank you.
 
A

Alf P. Steinbach

* (e-mail address removed):
Is it possible to const_cast a reference?

Yes, but if you seemingly need to, you're doing something wrong (most
likely).

If not , how can I remove to 'const' from a reference?

void aMethod2(A& a) {

}


void aMethod1(const A& a) {
// how can I call aMethod2
}

Why not let aMethod2 call aMethod1 instead?

Or, create a common member function (with const argument) called by both.
 
T

Tomás

class A {};

void aMethod2(A& a)
{

}


void aMethod1(const A& a)
{
A& modifiable = const_cast< A& > (a);

aMethod2( modifiable );
}

int main() {}


-Tomás
 
B

Ben Pope

Tomás said:
class A {};

void aMethod2(A& a)
{

}


void aMethod1(const A& a)
{
A& modifiable = const_cast< A& > (a);

aMethod2( modifiable );
}

int main() {}

Your variable name is misleading. Modifying the thing that a refers to,
through the reference modifiable will result in undefined behaviour.

It is not modifiable at all, it can merely be passed to a function
through an argument that was not declared const, but does not modify it.

Ben Pope
 
T

Tomás

Your variable name is misleading. Modifying the thing that a refers to,
through the reference modifiable will result in undefined behaviour.

It is not modifiable at all, it can merely be passed to a function
through an argument that was not declared const, but does not modify it.


It's *not* modifiable in the sense that it would be undefined behaviour to
do so.

It *is* modifiable in the sense that you can try modifiy it and the compiler
won't complain.

Sort of like when you're locked out of your house, and someone asks "Have
you got no way in?".

Non-undefined behaviour answer: "No."

What compiler will let you do answer: "Yeah I can smash the front window, or
tear down the sidewall."

First time I've drawn an analogy between the "Laws of Physics" and the "Laws
of the Compiler".

; P

-Tomás
 
B

Ben Pope

Tomás said:
It's *not* modifiable in the sense that it would be undefined behaviour to
do so.

It *is* modifiable in the sense that you can try modifiy it and the compiler
won't complain.

Sort of like when you're locked out of your house, and someone asks "Have
you got no way in?".

Non-undefined behaviour answer: "No."

What compiler will let you do answer: "Yeah I can smash the front window, or
tear down the sidewall."

First time I've drawn an analogy between the "Laws of Physics" and the "Laws
of the Compiler".

Well I guess "modifiable" is a tad more catchy than
"able_to_be_treated_as_non_const_just_dont_try_to_modify_it_for_fear_of_nasal_demons"

:p

Ben Pope
 
T

TB

Tomás skrev:
It's *not* modifiable in the sense that it would be undefined behaviour to
do so.

It *is* modifiable in the sense that you can try modifiy it and the compiler
won't complain.

But the execution environment might bitch like hell when you try
to write to a possibly read-only memory section, bringing down a
crucial real-time nuclear control network that takes 42 days to
reboot. I don't trust code that uses const_cast<>().
 
T

Tomás

I don't trust code that uses const_cast<>().


I think that that's everyone's first thought... until they find a
legitimate well-defined use for it.

I used to frown at the prospect of casting a Base to a Derived until I
found a legitimate need for it in one of my projects.

Derived& x = static_cast<Base&> (object);


-Tomás
 

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

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top