How to direct Foo ** pointer to const Foo ** in C ++

I have

class Fred { public: void inspect() const {}; void modify(){}; }; int main() { const Fred x = Fred(); Fred* p1; const Fred** q1 = reinterpret_cast<const Fred**>(&p1); *q1 = &x; p1->inspect(); p1->modify(); } 

How could const Fred ** q1 = & p1 be done via pointer casting?

(I just read that it is possible)

Thank you for your responses. Const_cast really works for objects

 #include <iostream> #include <stdio.h> using namespace std; class Fred { int a; public: Fred(){}; Fred(int a_input) { a = a_input; }; void inspect() const { cout << "Inspect called"<< endl; cout << "Value is "; cout << a << endl; }; void modify() { cout << "Modify called" << endl; a++; }; }; int main() { const Fred x = Fred(7); const Fred* q1 = &x; Fred* p1 = const_cast<Fred*>(q1); p1->inspect(); p1->modify(); p1->inspect(); x.inspect(); *p1 = Fred(10); p1->inspect(); } 

gives

 Inspect called Value is 7 Modify called Inspect called Value is 8 Inspect called Value is 8 Inspect called Value is 10 Inspect called Value is 10 

However, for predefined types, this does not work:

 int main() { const double a1 = 1.2; const double* b1 = &a1; cout << "a1 is " << (*b1) << endl; cout << "b1 is " << b1 << endl; double* c1 = const_cast<double*>(&a1); cout << "b1 is " << b1 << endl; cout << "c1 is " << c1 << endl; double* d1 = static_cast<double*>(static_cast<void*>(c1)); cout << "d1 is " << d1 << endl; cout<< "*d1 is " << *d1 << endl; *d1=7.3; cout<< "*d1 is " << *d1 << endl; cout<< "*d1 address is "<< d1 << endl; cout << "a1 is " << a1 << endl; cout << "a1 address is" << &a1 << endl; cout<< "*d1 is " << *d1 << endl; cout<< "*d1 address is "<< d1 << endl; double f1=a1; printf("f1 is %f \n", f1); } 

leads to:

 a1 is 1.2 b1 is 0xffbff208 b1 is 0xffbff208 c1 is 0xffbff208 d1 is 0xffbff208 *d1 is 1.2 *d1 is 7.3 *d1 address is 0xffbff208 a1 is 1.2 a1 address is0xffbff208 *d1 is 7.3 *d1 address is 0xffbff208 f1 is 1.200000 

Apparently, the g ++ compiler is optimized in such a way that it replaces a1 with 1.2 whenever it finds it, so even if its value on the stack has changed, it doesn't care.

(In my case, I had problems reading directly * b1, * c1, so I had to make a double static throw - the reinterpret did not work).

Is there any way to change a1 by compiling “normally”, therefore without compiling without optimization (so that I overtake the optimization effect)?

+4
source share
6 answers

You want const_cast .

+4
source

This should do it:

 Foo** f; const Foo** cf = const_cast<const Foo**>(f); 
+16
source

This is not a good idea, because it violates type safety. Let me explain why:

 Fred* pFred; const Fred** ppFred = const_cast<const Fred**>(&p); *ppFred = new const Fred; // Now pFred points to a const Fred pFred->some_evil_mutating_method(); // can do, since type of *pFred is non-const! 
+9
source

Why don't you just do :?

 const Fred** q1; *q1 = p1; 

Or do you want to rule out a constant violation without const_cast? “No sir, you cannot.”

+1
source

You must not do this. The fact that you cannot easily do the conversion is that it violates the constant correctness (and your code does this). Using the recommendations above, your code will compile and will call the mutating method on a permanent object (last line of code).

This is not recommended and in some rare cases it can even kill your application (a permanent global object can be stored on a read-only memory page) or leave it unstable (you change the internal state of the object by changing the permanent link to an internal member element, breaking invariants object).

About your problem: C ++ FAQ Lite [18.17]

0
source

you do not need to cast for const Fred** q1 = &p1 , since non-constant Fred** can be directly assigned to const Fred** q1 in its declaration.

-1
source

All Articles