2

AFAIK we cannot change a value of a constant variable in C. But i faced this interview question as below:

In C++, we have procedure to change the value of a constant variable. Could anybody tell me how could we do it?

5
  • 7
    i really wish interviewers would concentrate on more important things like whether the person has a passion for programming, curiosity for new things and has a certain amount of EQ as well. Commented Mar 5, 2011 at 16:57
  • Knowing about const_cast is a perfectly valid question in a series designed to test someones c++ knowledge. Problem arises when you only test for language knowledge. Commented Mar 5, 2011 at 16:58
  • @Erik so the interviewee is nervous and forgets about this particular thing in the interview and as such doesn't get the job, fair? More interesting is best practices in C++. Commented Mar 5, 2011 at 17:00
  • @Anders: Depends on the position. When we advertise for experienced senior c++ programmers, they better remember const_cast. A junior out of school, no, but they should answer some of the language-knowledge questions when we indicate in the ad that we want people with c++ knowledge Commented Mar 5, 2011 at 17:02
  • @Erik: At the same time, I use const_cast<> so rarely (I have used it only once) that I would have to look up the C++ standard every time I use it to make sure it doesn't invoke undefined behavior, despite the fact that I know very well what it does. And when I do use it, it's buried deep in library code. Commented Mar 5, 2011 at 17:06

6 Answers 6

8

You can modify mutable data members of a const-qualified class-type object:

struct awesome_struct { awesome_struct() : x(0) { } mutable int x; }; int main() { const awesome_struct a; a.x = 42; } 

The behavior here is well-defined.

Sign up to request clarification or add additional context in comments.

1 Comment

I had this in mind when writing my own post, but didn't know how to put it without using const member function. Good Job. +1!
7

Under the circumstances, I think I'd have explained the situation: attempting to change the value of a variable that's const gives undefined behavior. What he's probably asking about is how to change a variable that isn't itself const, but to which you've received a pointer or reference to const. In this case, when you're sure the variable itself is not const-qualified, you can cast away the const-ness with const_cast, and proceed to modify.

If you do the same when the variable itself is const-qualified the compiler will allow the code to compile, but the result will be undefined behavior. The attempt at modifying the variable might succeed -- or it might throw an exception, abort the program, re-format your NAS appliance's hard drives, or pretty much anything else.

It's probably also worth mentioning that when/if a variable is likely to need to be used this way, you can specify that the variable itself is mutable. This basically means that the variable in question is never const qualified, even if the object of which it's a part is const qualified.

Comments

6

There is no perfect way to cast away const-ness of a variable(which is const by definition) without invoking UB

Most probably the interviewers have no idea what they are talking about. ;-)

Comments

3

You cannot change the value of a constant variable. Trying to do so by cheating the compiler, invokes undefined behavior. The compiler may not be smart enough to tell you what you're doing is illegal, though!

Or maybe, you mean this:

const char *ptr= "Nawaz"; ptr = "Sarfaraz"; 

??

If so, then it's changing the value of the pointer itself, which is not constant, rather the data ptr points to is constant, and you cannot change that for example by writing ptr[2]='W';.

Comments

0

The interviewer was looking for const_cast. As an added bonus, you can explain scenarios where const_cast make sense, such as const_cast<MyClass *>(this)->doSomething() (which can be perfectly valid, although IMO not very clean), and scenarios where it can be dangerous , such as casting away constness of a passed const reference and therefore breaking the contract your functions signature provides.

Comments

0

If a const variable requires a change in value, then you have discovered a flaw in the design (under most, if not all, situations). If the design is yours, great! - you have an opportunity to improve your code.

If the design is not yours, good luck because the UB comment above is spot-on. In embedded systems, const variables may be placed in ROM. Performing tricks in an attempt to make such a variable writable lead to some spectacular failures. Furthermore, the problem extends beyond simple member variables. Methods and/or functions that are marked const may be optimized in such away that "lack of const-ness" becomes nonsensical.

(Take this "comment-as-answer" as a statement on the poor interview question.)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.