473,403 Members | 2,338 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

How can I const cast a reference

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.

Mar 4 '06 #1
7 4867
* Pl********@gmail.com:
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.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Mar 4 '06 #2
class A {};

void aMethod2(A& a)
{

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

aMethod2( modifiable );
}

int main() {}
-Tomás

Mar 5 '06 #3
Tomás wrote:
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
--
I'm not just a number. To many, I'm known as a string...
Mar 5 '06 #4
A& modifiable = const_cast< A& > (a);

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
Mar 5 '06 #5
Tomás wrote:
A& modifiable = const_cast< A& > (a);


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".


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
--
I'm not just a number. To many, I'm known as a string...
Mar 5 '06 #6
TB
Tomás skrev:
A& modifiable = const_cast< A& > (a);


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.


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<>().

--
TB @ SWEDEN
Mar 5 '06 #7
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
Mar 5 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Bolin | last post by:
Hi all, A question about smart pointers of constant objects. The problem is to convert from Ptr<T> to Ptr<const T>. I have look up and seen some answers to this question, but I guess I am too...
19
by: Christian Engström | last post by:
If you have a function that returns something by value, the gcc compiler (version 3.2.3 on Windows XP with MinGW) converts the returned value from the type you specify in the code, to the const...
5
by: klaus triendl | last post by:
hi, recently i discovered a memory leak in our code; after some investigation i could reduce it to the following problem: return objects of functions are handled as temporary objects, hence...
11
by: Mantorok Redgormor | last post by:
Is const really constant? And on an OT note: how can I post with a modified e-mail address so I don't get so much spam?
2
by: Pavel | last post by:
I am writing software for an embedded application and here is the question. GCC would emit data declared like const char text = "abc"; to .rodata (i.e. "read only data") section. I can put this...
8
by: Joseph Turian | last post by:
Some function requires a vector<const foo*> argument. How can I cast a vector<foo*> to vector<const foo*>? Thanks! Joseph
19
by: scroopy | last post by:
Is it impossible in C++ to create an assignment operator for classes with const data? I want to do something like this class MyClass { const int m_iValue; public: MyClass(int...
16
by: hzmonte | last post by:
Correct me if I am wrong, declaring formal parameters of functions as const, if they should not be/is not changed, has 2 benefits; 1. It tells the program that calls this function that the...
42
by: S S | last post by:
Hi Everyone I have const char *p = "Hello"; So, here memory is not allocated by C++ compiler for p and hence I cannot access p to modify the contents to "Kello" p = 'K'; // error at runtime
13
by: S James S Stapleton | last post by:
I have some code, and I want to make it future-resistant. I have a bunch of variables that are set up run-time, and once set up, should act as constants. I don't want to #define them, because their...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.