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

  • Thread starter Thread starter ORF
  • Start date Start date
  • Tags Tags
    Program
AI Thread Summary
In C++11, passing const arguments by reference can complicate situations where a variable needs to be modified. Labeling a variable as const prevents changes and informs the compiler to throw errors if modifications are attempted. It is generally unsafe to change a const variable, and there is no direct way to convert it to a non-const variable. However, there are methods to cast away const-ness, such as using C-style casts or C++'s const_cast, although these approaches can lead to undefined behavior. A safer alternative is to use the mutable keyword for class members, allowing modifications without breaking const correctness. Additionally, using unions can provide a workaround to modify a constant variable through a different name, though this comes with its own risks. Overall, while there are techniques to alter const variables, they should be used cautiously due to potential pitfalls and undefined behavior.
ORF
Messages
169
Reaction score
18
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
 
Technology 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 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 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 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 Silicon Waffle and ORF
Thank you for all the clarification, specially to @D H ;)

Greetings!
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...
Back
Top