All language subtitles for Type casting in C++- const_cast in C++ - 01_large

af Afrikaans
ak Akan
sq Albanian
am Amharic
ar Arabic
hy Armenian
az Azerbaijani
eu Basque
be Belarusian
bem Bemba
bn Bengali
bh Bihari
bs Bosnian
br Breton
bg Bulgarian
km Cambodian
ca Catalan
ceb Cebuano
chr Cherokee
ny Chichewa
zh-CN Chinese (Simplified) Download
zh-TW Chinese (Traditional)
co Corsican
hr Croatian
cs Czech
da Danish
nl Dutch
en English
eo Esperanto
et Estonian
ee Ewe
fo Faroese
tl Filipino
fi Finnish
fr French
fy Frisian
gaa Ga
gl Galician
ka Georgian
de German
el Greek
gn Guarani
gu Gujarati
ht Haitian Creole
ha Hausa
haw Hawaiian
iw Hebrew
hi Hindi
hmn Hmong
hu Hungarian
is Icelandic
ig Igbo
id Indonesian
ia Interlingua
ga Irish
it Italian
ja Japanese
jw Javanese
kn Kannada
kk Kazakh
rw Kinyarwanda
rn Kirundi
kg Kongo
ko Korean
kri Krio (Sierra Leone)
ku Kurdish
ckb Kurdish (Soranรฎ)
ky Kyrgyz
lo Laothian
la Latin
lv Latvian
ln Lingala
lt Lithuanian
loz Lozi
lg Luganda
ach Luo
lb Luxembourgish
mk Macedonian
mg Malagasy
ms Malay
ml Malayalam
mt Maltese
mi Maori
mr Marathi
mfe Mauritian Creole
mo Moldavian
mn Mongolian
my Myanmar (Burmese)
sr-ME Montenegrin
ne Nepali
pcm Nigerian Pidgin
nso Northern Sotho
no Norwegian
nn Norwegian (Nynorsk)
oc Occitan
or Oriya
om Oromo
ps Pashto
fa Persian
pl Polish
pt-BR Portuguese (Brazil)
pt Portuguese (Portugal)
pa Punjabi
qu Quechua
ro Romanian
rm Romansh
nyn Runyakitara
ru Russian
sm Samoan
gd Scots Gaelic
sr Serbian
sh Serbo-Croatian
st Sesotho
tn Setswana
crs Seychellois Creole
sn Shona
sd Sindhi
si Sinhalese
sk Slovak
sl Slovenian
so Somali
es Spanish
es-419 Spanish (Latin American)
su Sundanese
sw Swahili
sv Swedish
tg Tajik
ta Tamil
tt Tatar
te Telugu
th Thai
ti Tigrinya
to Tonga
lua Tshiluba
tum Tumbuka
tr Turkish
tk Turkmen
tw Twi
ug Uighur
uk Ukrainian
ur Urdu
uz Uzbek
vi Vietnamese
cy Welsh
wo Wolof
xh Xhosa
yi Yiddish
yo Yoruba
zu Zulu

Original subtitles

In this video we are going to study about const cast in C++. So its syntax is same as other types of cast.

So we specify the const cast keyword and then the type and then the expression or variable.

And const cast is used to cast away the constness of a variable. So constness of variable means if we have a variable, let's say const int *p.

Then it will mean that it's a constant integer pointer. So if we want to cast it to int * from const int * then we can use the const cast.

Now let's see what are all the different scenarios where we would need to cast away the constness of a variable.

So the first use case is that if we have some function let's say fun in this case and it expects an integer pointer.

See that it's not constant integer pointer it's a normal integer pointer. And we want to use this function in our code.

And we have a constant integer and we have a constant integer pointer. And we know that this function does not modify the values of the pointer that we are passing.

But here we have const in pointer then how can we use that. If we directly pass ptr in fun it will throw an error.

So before passing ptr here we need to cast away the constness of this variable into ptr1 and then we can pass it.

So we are thinking that why not just make it const int * instead of int *.

So it's quite possible to do that if it's your code if you are writing the code.

But sometimes you may be using some other libraries. So if you are writing your code it's not expected that you would change that library's code.

So you just need to modify your code and use the library as per its requirement. So that is a typical scenario.

Now another use can be that if we have a const member function and you need to modify a variable inside that member function.

So if you try to do this.x = i so you are modifying the value of x and it will throw a compile time error.

Why because inside this is a const member function and inside a const member function this pointer is treated as const pointer to const object of type this a.

So this is a constant pointer. So first you need to cast this pointer cast away its constness.

So we use const cast and the type of this pointer is the same as this class pointer.

So this will become normal a* and not const a*. So this is const a*. We have removed this const using this const cast.

And now we can change the value of x inside that class.

So let's see this in our code. So let's define the class a and it has a variable x and we have a public function void f

and we pass a value i to it and it's const.

And if we do so this x is same as simply writing x.

So we are assigning this passed value to this and then we create an object of this class and then we do a.f and we want it to 10.

Let's write a function to print the value of x also.

So it will simply print the value of x.

So after creating this a we print the value of x and after calling this function we again print its value and let's run it.

So you see that we are getting an error that cannot assign to a non static data member within a const member function f.

So let's modify it and do const cast here.

And now again let's run it and we see successfully that it compiles.

So in the constructor we have not defined any constructor so x was initialized to some value.

And then when we call this a.f 10 it will set the value of x to this passed value which is 10.

And then again we printed it and it correctly modifies this.

So in this case const cast was required.

Now let's keep in mind this important point about const cast that it is undefined behavior to try to modify a value that was initially declared as const.

So for example here we have a const integer a1 and its value is 10.

And we have a const integer pointer which is pointing to this a1.

So we have a1 somewhere in memory and its address is let's say 0x100 and we have saved a value of 10 here.

And p1 is a const pointer which is pointing here but we cannot modify it by writing *p1=20 because it will throw a compilation error that you cannot dereference and modify a const pointer.

So we cast away the constness of this p1 and save it into a variable another pointer p2.

So this is not constant this is normal integer pointer this is constant integer pointer.

So now we can assign a value to whatever p2 is pointing to because it's a normal integer pointer and we are allowed to do that.

So we think that we are modifying this value and setting it to 20.

So we are trying to play a trick here and trying to change the value of a const variable because this a1 was const int and it was assumed that its value will not change.

So what will happen if we try to print *p2 so remember p2 is pointing to this address 0x100 and we print *p2 it should print the modified value 20.

And this is same as a1 variable so if we try to print a1 this should also print the modified value but this does not happen.

So that's why it's called undefined behavior.

So now let's see it in the code and then you will see that it's not what we are expecting.

So we have a const int a and it's equal to 10 and we have const int pointer p1 which is pointing to a and then we have another int pointer p2.

And then finally we were setting this address value to 20 and then let's print out a and also print *p2.

And we would expect that both would be same but you see that a is 10 so it still prints 10 and *p2 is 20.

Now let's also print their addresses so we print the address of a and we also print *p2 to just verify that the address of a and *p2 both are same.

So we run it we see that these two values are same that means p2 is pointing to an address and that address is same as address of a1.

So both are same pointing to the same address in memory.

So is it possible that both are pointing to the same address but when we print the value a's value is 10 and *p2 value is 20.

How can the same memory address be storing two values.

So here what's happening is that memory address is holding this value 20 and this 10 comes from the fact that when we declare a variable as const int then the compiler will try to do optimization and wherever it will see a in the code it will try to replace it with its value.

So in fact the compiler is doing this during compilation.

So we are not printing the value stored at this address but we are just printing 10.

So even after modifying it it's still printing 10.

So that's why we should try to avoid these situations and we should not try to change the value of a const variable which was originally const and using const cast we are somehow trying to modify this value so this should not happen.

And it's mentioned in the C++ standard that except that any class member declared as mutable can be modified any attempt to modify a const object during its lifetime results in undefined behavior.

Now the last point is that const cast cannot cast to a type which is different from the original object type.

So if we have an integer here a and we have a const int pointer pointing to this a and we are trying to cast away the constness of this int * and save it to a normal character * so it will give compile time error that it's not permitted to cast a const int * to char *.

So this will give the error the compile time itself.

So that's all for const cast. See you in the next video. Thank you.

Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.