Ich habeWie Zeiger werfen Foo Foo ** const ** in C++
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();
}
Wie wäre es möglich, die const Fred ** q1 = & p1 über Zeiger-Casting zu tun?
(Ich habe gerade Lesung, dass dies möglich sein könnte)
Dank für Ihre Antworten danken. Die const_cast arbeitet in der Tat für Objekte
#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();
}
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
gibt jedoch für vordefinierte Typen funktioniert es nicht:
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);
}
wird dadurch:
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
Anscheinend das g ++ Der Compiler optimiert so, dass er a1 bei jeder Änderung durch 1,2 ersetzt. Selbst wenn sich sein Wert auf dem Stack geändert hat, ist das egal.
(In meinem Fall hatte ich Probleme mit dem direkten Lesen der * b1, * c1, also musste ich die doppelte statische Besetzung tun - die Reinterpret Cast funktionierte nicht).
Ist es eine Möglichkeit, wirklich a1 zu ändern, "normal" zu kompilieren, also nicht kompilieren ohne die Optimierung (also überhole ich den Optimierungseffekt)?
Danke für die Neuformatierung! Viel einfacher zu lesen! – Toji
Die neue Information, die Sie hinzugefügt haben, ist tatsächlich eine andere Frage. In Zukunft könnte es auch so aussehen (es wird auch mehr Antworten geben). Wie für die Compiler-Optimierungen für die Const-Primitiven: Wenn Sie nicht möchten, dass der Compiler es optimiert, um ... mach es nicht const. Der Punkt von const ist, dem Compiler zu sagen, dass Sie diesen Wert NICHT ändern werden. Wenn Sie danach versuchen, es zu ändern, verletzen Sie den C++ - Standard. Mit anderen Worten, tu es nicht! Noch einmal, wenn Sie beabsichtigen, einen Wert zu ändern, machen Sie es nicht const. Zeitraum. – Toji