Questions tagged [const-cast]

Anything related to the `const_cast` operation in C++, i.e. a form of compile-time conversion where the `const` or `volatile` qualifiers are cast away (removed) from a variable.

Anything related to the const_cast operation in C++, i.e. a form of compile-time conversion where the const or volatile qualifiers are cast away (removed) from a variable.

See CPPreference page on const_cast.

231 questions
96
votes
6 answers

Is const_cast safe?

I can't find much information on const_cast. The only info I could find (on Stack Overflow) is: The const_cast<>() is used to add/remove const(ness) (or volatile-ness) of a variable. This makes me nervous. Could using a const_cast cause…
Mag Roader
  • 6,850
  • 8
  • 32
  • 27
58
votes
2 answers

How to use const_cast?

I have a private variable in my Student class defined as: const int studentNumnber; I am trying to write a copy constructor for the Student and I need to cast away the constness to do this. Unfortunately, I don't understand how to use…
Sarah
  • 2,713
  • 13
  • 31
  • 45
51
votes
8 answers

Correct usage(s) of const_cast<>

As a common rule, it is very often considered a bad practice to use const_cast<>() in C++ code as it reveals (most of the time) a flaw in the design. While I totally agree with this, I however wonder what are the cases were using const_cast<>() is…
ereOn
  • 53,676
  • 39
  • 161
  • 238
38
votes
8 answers

Idiomatic way to create an immutable and efficient class in C++

I am looking to do something like this (C#). public final class ImmutableClass { public readonly int i; public readonly OtherImmutableClass o; public readonly ReadOnlyCollection r; public ImmutableClass(int i,…
lachy
  • 1,833
  • 3
  • 18
  • 27
31
votes
7 answers

How to call a non-const function within a const function (C++)

I have a legacy function that looks like this: int Random() const { return var_ ? 4 : 0; } and I need to call a function within that legacy code so that it now looks like this: int Random() const { return var_ ? newCall(4) : 0; } The problem…
Grammin
  • 11,808
  • 22
  • 80
  • 138
26
votes
1 answer

Why is (int&)0 ill-formed?

According to [expr.cast]/4, a C-style cast tries the following casts in order: const_cast static_cast static_cast followed by const_cast reinterpret_cast reinterpret_cast followed by const_cast The following cast is…
Brian Bi
  • 111,498
  • 10
  • 176
  • 312
22
votes
1 answer

Is it allowed to cast away const on a const-defined object as long as it is not actually modified?

Is the following allowed: const int const_array[] = { 42 }; int maybe_inc(bool write, int* array) { if (write) array[0]++; return array[0]; } int main() { return maybe_inc(false, const_cast(const_array)); } In particular, is it OK to…
BeeOnRope
  • 60,350
  • 16
  • 207
  • 386
21
votes
1 answer

Convert const char* to QString

I have to use the output of a function of a type const char* and I need to convert it to QString. Note: inside that function, these are lines of code to return the const char* char* ClassA::getData() const{ return const_cast
Mahmoud Hassan
  • 598
  • 1
  • 3
  • 15
21
votes
2 answers

C++ difference between adding const-ness with static_cast and const_cast of "this" object?

As per Scott Meyers, to prevent repetition of code in the const version of a getter and the non-const version of a getter, call the const version of the method from the non-const version: static_cast(*this).Methodology(); however, in…
Casey
  • 10,297
  • 11
  • 59
  • 88
20
votes
3 answers

Why does const_cast remove constness for a pointer but not for a pointer to a const?

I understand that const_cast works with pointers and references. I'm assuming that the input to const_cast should be a pointer or reference. I want to know why it doesn't remove the constness if the input is a pointer/reference to a const int? The…
r18ul
  • 1,082
  • 2
  • 12
  • 30
19
votes
2 answers

Am I right in saying that const_cast followed by modification on a ref-to-const bound to a temporary is okay?

I would like to check my understanding and conclusions on this matter. On IRC, it was asked: Is it acceptable to const_cast a const reference that's bound to a temporary object? Translating: he has a ref-to-const bound to a temporary, and he…
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
18
votes
2 answers

How to convert "pointer to pointer type" to const?

With the following code void TestF(const double ** testv){;} void callTest(){ double** test; TestF(test); } I get this: 'TestF' : cannot convert parameter 1 from 'double **' to 'const double **' I cannot understand why. Why test cannot…
jimifiki
  • 5,377
  • 2
  • 34
  • 60
18
votes
9 answers

C++: Why is const_cast evil?

I keep hearing this statement, while I can't really find the reason why const_cast is evil. In the following example: template void OscillatorToFieldTransformer::setOscillator(const SysOscillatorBase &src) { oscillatorSrc =…
The Quantum Physicist
  • 24,987
  • 19
  • 103
  • 189
15
votes
1 answer

What is a multilevel pointer?

When reading about const_cast I came across sentences like the following: Only the following conversions can be done with const_cast. In particular, only const_cast may be used to cast away (remove) constness or volatility. 1) Two possibly…
Claudiu
  • 224,032
  • 165
  • 485
  • 680
13
votes
1 answer

Modifying element of const std::vector via const_cast

Does the following program have undefined behavior? #include #include struct Foo { const std::vector x; }; int main() { std::vector v = {1,2,3}; auto f = new Foo{v}; const_cast(f->x[1]) = 42; //…
Arch D. Robison
  • 3,829
  • 2
  • 16
  • 26
1
2 3
15 16