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
0
votes
1 answer

testing for storage location of static const member

I have a class A { private: static const int b = 10; public: static void testReadOnly(int _b) { const_cast(A::b) = _b; } }; and I want to test whether the member variable b is initialized at compile time…
user2950911
  • 873
  • 3
  • 12
  • 19
0
votes
1 answer

static const to an object in a shared library in C++. Is it share between processes?

I've been writing a shared library in C++, but I want to share some instance of a class through users of the library. I mean, a read-only object loaded just one time from the library and used by every process linked to the library. As far as I know…
Daniel
  • 2,657
  • 1
  • 17
  • 22
0
votes
1 answer

overloaded const and non-const class methods returning references in C++

I have a data-structure class in C++ with an accessor to some object (may be large) and I have const and non-const methods using this accessor so I need to overload it. I am looking for a critique of the code below - maybe there is a way to…
John
  • 1,709
  • 1
  • 24
  • 27
0
votes
1 answer

Explanation of the UB while changing data

I was trying to demonstrate to a work pal that you can change the value of a constant-qualified variable if really wants to (and knows how to) by using some trickery, during my demostration, I've discovered that exists two "flavours" of constant…
PaperBirdMaster
  • 12,806
  • 9
  • 48
  • 94
0
votes
2 answers

Why I am able to modify const variable through classes and not in plain code. C++

When I write this code fragment, after modifying the value of const variable i through pointer I am getting value of i as 10, but when i print *ptr i get 110. const int i = 10; int *ptr = const_cast(&i); *ptr = *ptr + 100; cout << "i: "…
Daemon
  • 1,575
  • 1
  • 17
  • 37
0
votes
3 answers

Behaviour about const_cast<>

I have written a small problem for checking the behavior of const_cast on const data member. using namespace std; class myString{ public: myString(char * str) { p=str; …
dead programmer
  • 4,223
  • 9
  • 46
  • 77
0
votes
3 answers

Got confused with const_cast

Sample_Program-1 #include using namespace std ; int main(){ const int i = 9; int *j = const_cast(&i); //Ok int *j = const_cast(i); //Error } Sample_Program-2 #include using namespace std ; int main(){ const…
Viku
  • 2,845
  • 4
  • 35
  • 63
0
votes
1 answer

Using const_cast in factory model

Forgive/correct me if my nomenclature is incorrect. I have never understood the use of const_cast. Generally speaking, it seems to me that if you must use const_cast then your class/methods is probably fundamentally flawed unless you're using legacy…
Phlucious
  • 3,704
  • 28
  • 61
0
votes
1 answer

const_cast: override a const status

I am experimenting with const_cast operator and trying to override a const status of the parameter o passed as an argument: void function1 (const Object *o) { ... function2( const_cast < Object *> ( o ) ); //Exception in g++ } void…
justik
  • 4,145
  • 6
  • 32
  • 53
0
votes
3 answers

Const_cast allows to modify the constness to a path that leads to a const ?

Given this code (from my last post here): const int j = 5; // constant object const int *p = &j; // `p` is a const access path to `j` int *q = const_cast(p); // `q` is a non-const access path to `j` *q = 10; cout << *q <<…
JAN
  • 21,236
  • 66
  • 181
  • 318
-1
votes
2 answers

Why isn't assigning c*=40 change the value of a?

#include using namespace std; int main() { const int a = 20; const int* b = &a; cout<<"b* = "<<*b<<"\n"; int* c=const_cast(b); *c=40; cout<<"b* = "<<*b<<" a = "<
-1
votes
2 answers

using const_cast to pass const data argument to a function whose parameter is a non-const

We can use const_cast to pass const data argument to a function whose parameter is a non-const. int fun(int* ptr) { return (*ptr + 10); } int main(void) { int val = 10; const int *ptr = &val; int *ptr1 = const_cast (ptr); …
-2
votes
1 answer

How const prevents writing to specific memory [Clang, Mac OS]

I'm messing around with C++, reading some books about good habits in this language. I had read about const_cast, and wrote simple program which answer question : can I strip const prefix and write value to this address ? My code : #include…
Domin568
  • 31
  • 8
-2
votes
3 answers

const_cast usage giving me runtime exception

i have just started learning/exploring about const_cast and was experimenting with it when i found that after using const_cast,i get runtime error if i try to modify the data. Basically i am dong something like void main() { const char* str =…
bourne
  • 1,083
  • 4
  • 14
  • 27
-2
votes
3 answers

const_cast and const pointer to const vaiable

How is it possible that the value of *p and the value of DIM are different but the have the same address in memory? const int DIM=9; const int *p = &DIM; * (int *) p = 18; //like const_cast cout<< &DIM <<" "<< p << '\n'; cout << DIM << " …
1 2 3
15
16