如何在C++中将Foo**指针转换为const Foo**

7 c++ pointers casting

我有

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();
}
Run Code Online (Sandbox Code Playgroud)

怎么可能通过指针式转换来执行const Fred**q1 =&p1?

(我刚刚读到这可能是可能的)

谢谢您的回答.const_cast确实适用于对象

#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();
}
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

但是,对于预定义类型,它不起作用:

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);
}
Run Code Online (Sandbox Code Playgroud)

导致:

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 
Run Code Online (Sandbox Code Playgroud)

显然,g ++编译器会优化它,只要它找到它就会将a1替换为1.2,因此,即使它在堆栈上的值发生了变化,它也无关紧要.

(在我的情况下,我遇到了直接读取*b1,*c1的问题,所以我不得不进行双重静态演员 - 重新解释演员不起作用).

有没有办法真正改变a1,编译"正常",因此没有优化就不编译(所以我超越了优化效果)?

Toj*_*oji 17

这应该这样做:

Foo** f;
const Foo** cf = const_cast<const Foo**>(f);
Run Code Online (Sandbox Code Playgroud)


Pav*_*aev 10

这不是一个好主意,因为它违反了类型安全.让我解释一下原因:

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!
Run Code Online (Sandbox Code Playgroud)

  • 你警告远离这种行为是正确的,但请不要将"不是一个好主意"误认为"不可能".在代码中用const_cast替换reinterpret_cast会产生完全有效的,可编译的C++. (2认同)
  • 是的,刚检查了标准.我的错 - 我从来没有意识到const_cast特别适用于指针链,尽管它本身就不安全.Ironic,考虑到一般认为`const_cast`只有当你从一个固有const的对象中抛弃const而不安全时 - 在这种情况下,当你将const"加"到一个非const的对象时它是不安全的.对我来说这看起来像是一个非常臭的设计决定,但我想这对你来说是C++.将解决问题. (2认同)