We have one more use of constant cast. If you have a constant member function inside a class then you will have read only permission, you can't update a variable value inside that function. In this situation we can use constant cast as well.
Yes that's correct but this sentence is confusing, because it doesn't allow to remove the constness of current variable/pointer/reference actually when you copy const to non const which is not allowed then it is useful.
cast away is wrong usage. in C,int typecast is done by (int)X where X can be a non int. By adding const to it, X becomes a const int. it makes a variable non mutable const int.
This is amazing! Never knew this and could not even think of this ever. const_cast should have been prohibited for the use-case when the referenced object is const. Any specific reason, why it still allows it?
Actually const values was replaced at compile time only, so where ever you are using them as value you will only get the value which you assigned in the beginning when you made it const.
Time 6.20 min. The output of a1=10; seems ok as compiler optimized the code since the variable was declared as constant. But the second part is still unclear to me how "*d" is printing as 15.
Because *d1 is actually pointing to address of a1 and when we assigned *d1=15; //line number 16, then we actually changed a1 value. And if we are printing *d1 which is pointing to address of a1 and a1 has been changed to 15, we are getting 15.
the only thing which is GUARANTEED by C/C++ is that when you have declared some variable constant you can not change that value by using that same variable, but if we get the address of the place where that variable is storing that const value you can change it, like i did (so we are changing a1 using d1 and it is not violating any rule) :D.
Changing const variable using its address is undefined behavior. I have clearly mentioned in video we should never use const_cast to remove const from variable so that we can change it. Rather it's use is to pass in some function which requires non const variable where as you have const variable. And i think you can not stop someone from changing const variable using const_cast. At least not for basic data types but you can stop in case of classes by simply not allowing to take address of your object. (This is my today's topic, don't miss that video :) In case someone interested : czcams.com/video/IAWoPgXtSNA/video.html And i am not sure about we can overload const_cast, would let you know if i will find something.
#2 is terrible advise! The whole point is that you don't know the implementation of the 3rd party lib method, hence casting away constness can easily get you into the gotcha you explained in example #1.
I believe this won't work if the 'const int a1' is a global variable? Is this because global const are stored in initialized DATA SEGMENT in cpp memory structure? Correct me if i am wrong.
Notice the point that it doesn't work on constant, it only works when original variable is non const and you get its adress as const now you want to remove const from this adress then it is defined behaviour, otherwise changing const thing is undefined behaviour.
yes you are correct when we want to change global const then we won't be able to change it because it is stored in read only section but when it comes to local const variable the scenarios are different, local const variables are stored on stack so its ok you can change that, I think this will clear your doubt.
this code should create compilation error in my opinion. the line just had const_cast right before and the following line goes against it and assigns a value.
Hi everyone, Don't forget to hit LIKE and SUBSCRIBE button for more videos like this!!
And this will help me a-lot.
6:16 the value actually stored in a is 15 after it has been modified.
We have one more use of constant cast. If you have a constant member function inside a class then you will have read only permission, you can't update a variable value inside that function. In this situation we can use constant cast as well.
What you do bro???
Method of showing a common real world use case is very helpful. Good work, thank you.
in simple words, const_cast is used to cast away (remove) constness or volatility of variable/pointer/reference.
Yes that's correct but this sentence is confusing, because it doesn't allow to remove the constness of current variable/pointer/reference actually when you copy const to non const which is not allowed then it is useful.
cast away is wrong usage. in C,int typecast is done by (int)X where X can be a non int. By adding const to it, X becomes a const int. it makes a variable non mutable const int.
Thaks for the awesome explanation
How can we be sure that the third party library doesnt change the variable pointed to by the pointer? we may not have access to the code.
It will most likely be mentioned in the documentation of the function whether it changes the passed value or not.
Thank you very much :) You did a great job.
Thanks man!!
This is amazing! Never knew this and could not even think of this ever. const_cast should have been prohibited for the use-case when the referenced object is const. Any specific reason, why it still allows it?
very well explained sir
Thanks..
Good
In first scenario, you have pointer to constant integer. But you are speaking as constant pointer. Correct it
Happens..
voice quality in this video is not as usual like other videos. I find it difficult to understand some words you spoke in the video
That time I did not have good set up.
Appreciated, good job !!!!!!!!!
Glad it helped!
How we will know , if third party library is changing my pointer or not? (as you told, we don't know the implementation of third party library)
I can't even understand the first part lol
Why are (a1) and (*d1) values different when they have the same memory address?
Actually const values was replaced at compile time only, so where ever you are using them as value you will only get the value which you assigned in the beginning when you made it const.
@@CppNuts Okay, so you can not change const values. But why does it "appear" to be changing then?
I think this (a1) and (*d1) confusion wasn't removed satisfactorily.
6:09 here should be that i think, "instead of 10 now a1 which is this 0xFF address is actually holding 15" r8??
this is not clear. he skips it.
Time 6.20 min. The output of a1=10; seems ok as compiler optimized the code since the variable was declared as constant.
But the second part is still unclear to me how "*d" is printing as 15.
Because *d1 is actually pointing to address of a1 and when we assigned *d1=15; //line number 16, then we actually changed a1 value. And if we are printing *d1 which is pointing to address of a1 and a1 has been changed to 15, we are getting 15.
Yes, you are right, but my intention was to know since a1 is constant , it shouldn't have been modified.
the only thing which is GUARANTEED by C/C++ is that when you have declared some variable constant you can not change that value by using that same variable, but if we get the address of the place where that variable is storing that const value you can change it, like i did (so we are changing a1 using d1 and it is not violating any rule) :D.
CppNuts how to avoid this scenario of changing const variable using the address?
Changing const variable using its address is undefined behavior. I have clearly mentioned in video we should never use const_cast to remove const from variable so that we can change it.
Rather it's use is to pass in some function which requires non const variable where as you have const variable.
And i think you can not stop someone from changing const variable using const_cast. At least not for basic data types but you can stop in case of classes by simply not allowing to take address of your object. (This is my today's topic, don't miss that video :)
In case someone interested : czcams.com/video/IAWoPgXtSNA/video.html
And i am not sure about we can overload const_cast, would let you know if i will find something.
#2 is terrible advise! The whole point is that you don't know the implementation of the 3rd party lib method, hence casting away constness can easily get you into the gotcha you explained in example #1.
thanks a lot sir
You are welcome!!
This video is nice.. How about other casting ? I cudn't get.
Hi Rahul Kumar Agrawal, Yet to shoot them, you will get it soon, stay tuned. :D
you said d1 ultimately changed a1's address and a1 actually holding 10 not 15 , it is supposed to be 15(at a1)???
I believe this won't work if the 'const int a1' is a global variable? Is this because global const are stored in initialized DATA SEGMENT in cpp memory structure? Correct me if i am wrong.
Notice the point that it doesn't work on constant, it only works when original variable is non const and you get its adress as const now you want to remove const from this adress then it is defined behaviour, otherwise changing const thing is undefined behaviour.
const int a1 = 10;
int main()
{
const int *b1 = &a1;
int *d1 = const_cast(b1);
*d1 = 15;
cout
yes you are correct when we want to change global const then we won't be able to change it because it is stored in read only section but when it comes to local const variable the scenarios are different, local const variables are stored on stack so its ok you can change that, I think this will clear your doubt.
Yes. Thanks alot :-)
this code should create compilation error in my opinion. the line just had const_cast right before and the following line goes against it and assigns a value.
expalination is confusing
Your content is very good, but please improve the sound quality and speak slow...
Thanks..
sure i will try.
constant_cast works fine even when the original variable is constant, I just ran the code and works fine for me.
For you, this time, with this compiler.
Its Undefined Behavior and should not be counted upon.
Explanation is not clear.
Which part?
The sound is not clear. I listened again with earphones in silence only then I understood it.