const-cast

Strange behavior of const_cast [duplicate]

半腔热情 提交于 2019-11-27 09:38:24
This question already has an answer here: Two different values at the same memory address 6 answers Consider the following code: I declare a new reference end assign it to value a via const_cast. Then I just increase the reference value print the addresses and values. #include <iostream> using namespace std; int main() { const int a = 7; int &b = const_cast<int&>(a); ++b; cout<<"Addresses "<<&a<<" "<<&b<<endl; cout<<"Values "<<a<<" "<<b<<endl; } //output Addresses 0x7fff11f8e30c 0x7fff11f8e30c Values 7 8 How can i have 2 different values in the same address?? Modifying a constant object gives

How to convert “pointer to pointer type” to const?

你离开我真会死。 提交于 2019-11-27 09:03:25
With the following code void TestF(const double ** testv){;} void callTest(){ double** test; TestF(test); } I get this: 'TestF' : cannot convert parameter 1 from 'double **' to 'const double **' I cannot understand why. Why test cannot be silently casted to const double** ? Why should I do it explicitly? I know that TestF(const_cast<const double**>(test)) makes my code correct, but I feel this should be unnecessary. Are there some key concepts about const that I'm missing? AnT The language allows implicit conversion from double ** to const double *const * , but not to const double ** . The

const_casting question [duplicate]

落花浮王杯 提交于 2019-11-27 07:26:31
问题 This question already has an answer here: Two different values at the same memory address 6 answers I have the following code: int main(){ const int a = 1; const int* b(&a); int* c = const_cast<int*>(b); *c = 29; cout<<*c<<a<<*b; return EXIT_SUCCESS; } Why doesnt the value of 'a' change to 29? Does this mean that the constness of a is not removed when const_casting b? 回答1: Constant variables also allows the compiler certain optimizations, one of these is that the compiler can keep the value

Can we use `const_cast` to modify a constant variable? [duplicate]

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-27 06:53:33
问题 This question already has answers here : Two different values at the same memory address (6 answers) Closed 2 years ago . int main() { const int ia = 10; int *pia = const_cast<int*>(&ia); *pia = 5; std::cout << &ia << "\t" << pia <<endl; std::cout << ia << "\t" << *pia <<endl; return 0; } The output is: 0x28fef4 0x28fef4 10 5 *pia and ia have the same address, but they have different values. My purpose is to use const_cast to modify a constant value, but as the result shows that it does not

How to use const_cast?

删除回忆录丶 提交于 2019-11-27 06:27:57
I have a private variable in my Student class defined as: const int studentNumnber; I am trying to write a copy constructor for the Student and I need to cast away the constness to do this, unfortunately I don't understand how to use std::const_cast . This is what I am trying to do in my copy constructor: Student(const Student & s) : Person(p.getName(), p.getEmailAddress(), p.getBirthDate()), school(0), studentNumber(0) { school = new char[strlen(s.school) + 1]; strcpy_s(school, strlen(s.school) + 1, s.school); const_cast<int*>(this)->studentNumber = s.studentNumber; //studentNumber = s

Is it safe to remove const via const_cast and invoke a non-const function that does not modify the resulting object?

僤鯓⒐⒋嵵緔 提交于 2019-11-27 06:00:30
问题 I know that casting away const -ness should be done with care, and any attempt to remove const -ness from an initially const object followed by modifying the object results in undefined behaviour. What if we want to remove const -ness so that we may invoke a non-const function which doesn't modify the object? I know that we should actually mark such function const , but suppose I'm using a "bad" code that doesn't have the const version available. So, to summarize, is the code below "safe"? My

How to call a non-const function within a const function (C++)

陌路散爱 提交于 2019-11-27 05:54:10
问题 I have a legacy function that looks like this: int Random() const { return var_ ? 4 : 0; } and I need to call a function within that legacy code so that it now looks like this: int Random() const { return var_ ? newCall(4) : 0; } The problem is that I'm getting this error: In member function 'virtual int Random() const': class.cc:145: error: passing 'const int' as 'this' argument of 'int newCall(int)' discards qualifiers Now I know in order to fix this error I can make my newCall() a const

Correct usage(s) of const_cast<>

徘徊边缘 提交于 2019-11-27 04:17:25
问题 As a common rule, it is very often considered a bad practice to use const_cast<>() in C++ code as it reveals (most of the time) a flaw in the design. While I totally agree with this, I however wonder what are the cases were using const_cast<>() is ok and the only solution . Could you guys please give me some examples you know/you encountered ? Thank you very much. 回答1: const_cast is also used to remove volatile modifiers, as put into practice in this (controversed) article: http://www.drdobbs

Why can't a const method return a non-const reference?

夙愿已清 提交于 2019-11-26 23:25:27
问题 Why won't the method getRanks() below compile, and how can I fix it gracefully? All I want do is define a member accessor method that returns a reference to a member. The reference is not const since I might well modify what it refers to later. But since the member method does not modify the object, I declare it const . The compiler (clang, std=c++11) then insists that there is a "binding of reference" that "drops qualifiers". But I'm NOT dropping qualifiers, am I? And if I am, why: struct

Strange behavior of const_cast [duplicate]

廉价感情. 提交于 2019-11-26 14:49:28
问题 This question already has answers here : Two different values at the same memory address (6 answers) Closed 2 years ago . Consider the following code: I declare a new reference end assign it to value a via const_cast. Then I just increase the reference value print the addresses and values. #include <iostream> using namespace std; int main() { const int a = 7; int &b = const_cast<int&>(a); ++b; cout<<"Addresses "<<&a<<" "<<&b<<endl; cout<<"Values "<<a<<" "<<b<<endl; } //output Addresses