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

C++ modifying const object through const_cast pointer works

I have this piece of code: #include using namespace std; class X { public: const int x; X(int i) : x(i) { } int getX() const { return x; } }; int main() { const X d(45); …
SexyBeast
  • 7,913
  • 28
  • 108
  • 196
-2
votes
1 answer

Why can't I change the value of a const data (int in my case) using const_cast

I have a piece of program as shown below: #include #include using namespace std; int main() { const int i = 100; cout<<"const i::"<(i) = 200; cout<<"const i after cast::"<
anilLuwang
  • 25
  • 4
-2
votes
3 answers

const_cast failing in c++

CallingClass::CallingFunc() { SomeClass obj; obj.Construct(*Singleton::GetInstance()); // passing the listener // Singleton::GetInstance() returns a static pointer. //Singleton is derived from IListener } SomeClass::Construct(const…
yogesh singh
  • 101
  • 1
  • 9
-3
votes
3 answers

Unhandled exception while over-writing value after removing constness

Below is the code :- const int temp=100; class A { public: void fun(int& temp) { cout<<"am inside fun()"<
ravi
  • 10,994
  • 1
  • 18
  • 36
-3
votes
2 answers

C++ const_cast does not remove the const-ness of variable

I tried executing the following program. #include using namespace std; int main() { const int a = 0; cout << &a <(&a); *ptr = 2; cout << ptr <
user1414696
  • 307
  • 4
  • 15
-6
votes
1 answer

C++ 11: conversion const int* to int* using unordered_set::push

I have this problem of conversion with this code using c++11 standard: #include struct B { int x, y; }; class A { struct hash { std::size_t operator()( int* const a ) const { return std::hash()( *a…
xgbuils
  • 115
  • 1
  • 9
1 2 3
15
16