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
4
votes
2 answers

Can you construct a string from `volatile const char*` ? (without using `const_cast`)

Basicly I have volatile const char* and want to create a string. The volatile keyword here is in all likelyhood irrelevant(a misunderstanding from a previous dev), but cannot easily get rid of it (coming from other library). cannot convert argument…
darune
  • 10,480
  • 2
  • 24
  • 62
4
votes
2 answers

Automatic type deduction with const_cast is not working

In my work the use of const_cast is under some circumstances unavoidable. Now I have to const_cast some pretty complicated types and actually I don't want to write all this type clutter in the const_cast expressions, especially if Clutter…
Aleph0
  • 5,816
  • 4
  • 29
  • 80
4
votes
4 answers

const_cast setting a rule and breaking it for function const

In the following example I found on the net, it is mentioned that one of the advantages of const_cast is that it allow a constant function changes the class members. It is a question to me. Why should we set a rule for a function by const and then…
barej
  • 1,330
  • 3
  • 25
  • 56
4
votes
1 answer

Is const_cast on pointer to member safe?

In the following code, a non-const method of an object calls a const-method of the same object that returns a const-pointer to the object's field, and then this returned pointer is casted to a non-const version — it's a technique similar to one in…
Joker_vD
  • 3,715
  • 1
  • 28
  • 42
4
votes
4 answers

Why can't a const method return a non-const reference?

Why won't the method getRanks() below compile, and how can I fix it gracefully? All I want do is define a member accessor method that returns a reference to a member. The reference is not const since I might well modify what it refers to later. But…
kdog
  • 1,583
  • 16
  • 28
4
votes
2 answers

Can not use dynamic_cast to a const object

I want to write a method where a Base object pointer will be passed as a parameter, and inside the method it will be casted to derived object pointer. void func( const Base* const obj){ Derived* der = dynamic_cast(obj); } But it shows…
Rakib
  • 7,435
  • 7
  • 29
  • 45
4
votes
2 answers

Using const_cast to add const-ness - bad idea?

as we all know the usage of const_cast to remove the const-ness of a pointer should be avoided. But how is it about the other way around? For my use case I have a function that copies data (bytes) from a non-const source buffer. I thought a good…
Steve Murdock
  • 709
  • 1
  • 10
  • 20
4
votes
3 answers

Is there a better way to initialize reference members to reference another member in the same class

Before anyone says anything I know this is probably not recommended but I am still curious if there is a better way to do it or reasons not to beyond just it's a strange thing to do. I started looking into this because I wanted to access elements of…
Robert F.
  • 63
  • 5
4
votes
1 answer

inout-parameter - replace one const-handle with another

In an object, I have an array of const-handles to some object of another specific class. In a method, I may want to return one of this handles as an inout-parameter. Here as a simplified example: class A {} class B { const(A) a[]; this() {…
Peter Schneider
  • 1,683
  • 12
  • 31
3
votes
2 answers

Why const_cast away volatile only work for pointer

// OK! volatile CString* a0; CString* a1 = const_cast(a0); // error C2440: 'const_cast' : cannot convert from 'volatile CString' to 'CString' volatile CString b0; CString b1 = const_cast(b0); I was wondering, why const_cast…
Cheok Yan Cheng
  • 47,586
  • 132
  • 466
  • 875
3
votes
3 answers

Why is only static_cast able to return new object of requested type?

Among static_cast, dynamic_cast, reinterpret_cast and const_cast, only static_cast is able to return an object of desirable type, whereas the other type can return only pointer or reference to representation. Why is it so? Examples: int y =…
3
votes
1 answer

Why does const_casting a heap.top() of priority_queue have undefined behavior?

I've made a simple Huffman encoding program to output individual encodings for characters and save the encoded file. This was for an assignment, and I was told that using const_cast on heap.top() is considered undefined behavior if we heap.pop()…
Jack Avante
  • 1,405
  • 1
  • 15
  • 32
3
votes
1 answer

Why type casting of const void* is legal in C not C++ without using static_cast

There are a couple of questions with similar names on StackOverflow, such as this one and this one .etc However, none of them answered the questions in my mind... Problem Background: I'm including a C header file in C++ code. The problematic C code…
ZR_xdhp
  • 361
  • 2
  • 13
3
votes
2 answers

Why doesn't const_cast work on arguments to std::function?

I'm providing a const and non-const variation of a member function where I reuse the const version to implement the non-const version as described in this answer per Scott Meyers books. The const version takes an argument of type: const…
WilliamKF
  • 41,123
  • 68
  • 193
  • 295
3
votes
1 answer

Unexpected behavior involving const_cast

I came up with the following example, which exposes some unexpected behavior. I would expect that after push_back, whatever is in the vector is there. It looks like the compiler somehow decided to re-use memory used by str. Could someone explain…