tags:

views:

245

answers:

7

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 would it be possible to do the const Fred** q1 = &p1 via pointer-casting?

(I have just been reading that this might be possible)

Thank you for your answers. The const_cast works indeed 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 it 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);
}

is resulting in:

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 optimizes such that it replaces a1 by 1.2 whenever it finds it, so, even if its value on the stack has changed, it does not care.

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

Is it any way to really change a1, compiling "normally", therefore not compiling without the optimization (so I overtake the optimization effect)?

A: 

You want const_cast.

Frank Krueger
allows you to create a non-const pointer to a const object, but any attempt to actually change that const object still results in undefined behavior.
Ben Voigt
+9  A: 
Foo** f;
const Foo** cf = const_cast<const Foo**>(f);

Should do it

Toji
+7  A: 

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!
Pavel Minaev
You are correct in warning away from this type of behavior, but please do not mistake "not a good idea" for "not possible". Replacing reinterpret_cast with const_cast in his code produces perfectly valid, compilable C++.
Toji
Yup, just checked the Standard. My fault - I had never realized that const_cast is specifically well-defined for pointer chains despite being inherently unsafe there. Ironic, considering that it is generally assumed that `const_cast` is unsafe only when you cast away constness from an object that is inherently const - while in this case, it is unsafe when you "add" const to an object which is non-const. It looks like a very smelly design decision to me, but I guess that's C++ for you. Will fix the answer.
Pavel Minaev
@Pavel, if you need a const_cast, you are doing something potentially dangerous, the safe cases don't need the cast. (Note that in C, there are safe cases which need a cast).
AProgrammer
This isn't true. For example, you may need a `const_cast` to invoke a const overload, or a const member method, when you would otherwise get non-const version. This is perfectly safe, and not even potentially dangerous.
Pavel Minaev
I get the impression that Pavel understands that principle fairly well, he's just voicing it to the OP.
Toji
I think AProgrammer had *removing* const with `const_cast` in mind. For *adding* const, you can use something else instead of the cast, like `A const a.doit();`.
Johannes Schaub - litb
You can, though I don't see any reason for introducing an extra local when there's a cast available for just that purpose :)
Pavel Minaev
+1  A: 

Why don't you just make:?

const Fred** q1;
*q1 = p1;

Or you want to elide constness violation without const_cast? -- no, sir, you can't.

Pavel Shved
A: 

you shouldn't need to do any casting for const Fred** q1 = &p1 as a non-const Fred** can be directly assigned to a const Fred** q1 in its declaration.

Jeff Leonard
Nope, it cannot. Try it for yourself - it won't compile without a `const_cast`.
Pavel Minaev
A: 

You should not do it. The fact that you cannot easily do the conversion is because it breaks constant correctness (and your code exercises it). Using the recommendations above your code will compile and will call a mutating method on a constant object (last line of code).

That is not recommended and in some rare cases could even kill your application (a constant global object could be stored in a read-only memory page), or leave it an unstable situation (you change the internal state of an object by changing through a constant reference into an internal member element, breaking the object invariants).

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

David Rodríguez - dribeas
A: 

const_cast does not help me to change predefined types.

Nothing makes it legal to change an object originally declared as const. Attempting to use const_cast to do so results in UNDEFINED BEHAVIOR.
Ben Voigt