Can the constness be changed in the middle of the program?

  • Context:
  • Thread starter Thread starter ORF
  • Start date Start date
  • Tags Tags
    Program
Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
5 replies · 2K views
ORF
Messages
169
Reaction score
19
Hello

I'm using C++11. I had a bug (about one variable which changed at some point ), so in order to find out where it is I pass all const arguments by reference (to all functions of the program). At one point one variable must be changed, and I wonder if there is a way to convert that const variable into a non-const one.

Thank you in advance

Greetings
 
Physics news on Phys.org
Labeling a variable as CONST is a preprocessor command.
This does a couple things.
First off it tells the compiler that if the code tries to change the value of that variable to throw an error.
Secondly (I think) it changes where, in the memory allocated to your program, that variable is stored.

I'm a little surprised honestly that the compiler would let you pass a CONST variable by reference,

if a variable has to be changed it is by definition NOT a CONST and you shouldn't label it as such. There is no safe way that I am aware of to change a value of a CONST.
 
  • Like
Likes   Reactions: ORF
Do you ever take the address and use a pointer to access the variable(s)? Maybe you have an improper cast?

const char foo[] = "hello";
char *bar = (char*) foo;
strcpy(bar, "world"); // Oops!
 
  • Like
Likes   Reactions: ORF
cpscdave said:
Labeling a variable as CONST is a preprocessor command.
The OP was asking about const rather than CONST. The former is a keyword in C and in C++. The latter, who knows what that means? Unless the programmer has #defined CONST to mean something, even the preprocessor doesn't know what CONST means. If you worked for me and you insisted on doing this, I would think of firing you unless you had a very good reason for doing so. For example, having to deal with a compiler last updated in the previous millennium would qualify as a justifiable excuse.
ORF said:
Hello
I'm using C++11. I had a bug (about one variable which changed at some point ), so in order to find out where it is I pass all const arguments by reference (to all functions of the program). At one point one variable must be changed, and I wonder if there is a way to convert that const variable into a non-const one.

There are multiple ways of achieving this. None are all that good.

Approach #1: Use a C-style cast to remove the const qualifier:
C:
void some_function(const SomeType& ref) {
    ((SomeType&)ref).something_i_want_to_change = 0;
}

Approach #2: Use a C++ style cast to remove the const qualifier:
C:
void some_function(const SomeType& ref) {
    const_cast<SomeType&>(ref).something_i_want_to_change = 0;
}

Approach #3: (Applicable to member data only): Mark a data member as mutable:
C:
class Foo {
public:
    some_function () const {
        something_i_want_to_change = 0;
    }
private:
    mutable int something_i_want_to_change;
};

The first is very hard to find. Those C-style casts that cast away const-ness are just bad. The latter two are easily found via grep.

Beautiful programming concepts aside, there are times when you either need to cast const-ness away (with a const_cast, not a C-style cast), or just magically making the const-ness disappear, which is what mutable does. Casting away const-ness is, by definition, undefined behavior. Marking a member as mutable (supposedly) is well-defined behavior. Beware, however, that a number of compilers do not treat mutable properly.
 
Last edited:
  • Like
Likes   Reactions: newjerseyrunner, ORF, Silicon Waffle and 1 other person
In C, you are allowed to weird things. On your own head be it! But, you could use a union:
Code:
union Uconst {
  const int c_I;
  int I;
  };
This allows you to declare a constant and modify it using another name...
 
  • Like
Likes   Reactions: Silicon Waffle and ORF
Thank you for all the clarification, specially to @D H ;)

Greetings!