Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,000 --> 00:00:09,440
In this video we are going to study about const cast in C++. So its syntax is same as other types of cast.
2
00:00:09,440 --> 00:00:15,320
So we specify the const cast keyword and then the type and then the expression or variable.
3
00:00:15,320 --> 00:00:27,840
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.
4
00:00:27,840 --> 00:00:40,440
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.
5
00:00:40,440 --> 00:00:47,440
Now let's see what are all the different scenarios where we would need to cast away the constness of a variable.
6
00:00:47,440 --> 00:00:58,440
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.
7
00:00:58,440 --> 00:01:07,840
See that it's not constant integer pointer it's a normal integer pointer. And we want to use this function in our code.
8
00:01:07,840 --> 00:01:18,840
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.
9
00:01:18,840 --> 00:01:26,240
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.
10
00:01:26,240 --> 00:01:35,640
So before passing ptr here we need to cast away the constness of this variable into ptr1 and then we can pass it.
11
00:01:35,640 --> 00:01:44,040
So we are thinking that why not just make it const int * instead of int *.
12
00:01:44,040 --> 00:01:51,040
So it's quite possible to do that if it's your code if you are writing the code.
13
00:01:51,040 --> 00:02:01,640
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.
14
00:02:01,640 --> 00:02:13,640
So you just need to modify your code and use the library as per its requirement. So that is a typical scenario.
15
00:02:13,640 --> 00:02:27,040
Now another use can be that if we have a const member function and you need to modify a variable inside that member function.
16
00:02:27,040 --> 00:02:38,040
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.
17
00:02:38,040 --> 00:02:58,040
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.
18
00:02:58,040 --> 00:03:10,040
So this is a constant pointer. So first you need to cast this pointer cast away its constness.
19
00:03:10,040 --> 00:03:18,040
So we use const cast and the type of this pointer is the same as this class pointer.
20
00:03:18,040 --> 00:03:32,040
So this will become normal a* and not const a*. So this is const a*. We have removed this const using this const cast.
21
00:03:32,040 --> 00:03:40,040
And now we can change the value of x inside that class.
22
00:03:40,040 --> 00:04:04,040
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
23
00:04:04,040 --> 00:04:10,040
and we pass a value i to it and it's const.
24
00:04:10,040 --> 00:04:17,040
And if we do so this x is same as simply writing x.
25
00:04:17,040 --> 00:04:30,040
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.
26
00:04:30,040 --> 00:04:37,040
Let's write a function to print the value of x also.
27
00:04:37,040 --> 00:04:47,040
So it will simply print the value of x.
28
00:04:47,040 --> 00:05:08,040
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.
29
00:05:08,040 --> 00:05:20,040
So you see that we are getting an error that cannot assign to a non static data member within a const member function f.
30
00:05:20,040 --> 00:05:32,040
So let's modify it and do const cast here.
31
00:05:32,040 --> 00:05:38,040
And now again let's run it and we see successfully that it compiles.
32
00:05:38,040 --> 00:05:44,040
So in the constructor we have not defined any constructor so x was initialized to some value.
33
00:05:44,040 --> 00:05:52,040
And then when we call this a.f 10 it will set the value of x to this passed value which is 10.
34
00:05:52,040 --> 00:05:57,040
And then again we printed it and it correctly modifies this.
35
00:05:57,040 --> 00:06:02,040
So in this case const cast was required.
36
00:06:02,040 --> 00:06:16,040
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.
37
00:06:16,040 --> 00:06:28,040
So for example here we have a const integer a1 and its value is 10.
38
00:06:28,040 --> 00:06:30,040
And we have a const integer pointer which is pointing to this a1.
39
00:06:30,040 --> 00:06:38,040
So we have a1 somewhere in memory and its address is let's say 0x100 and we have saved a value of 10 here.
40
00:06:38,040 --> 00:06:55,040
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.
41
00:06:55,040 --> 00:07:05,040
So we cast away the constness of this p1 and save it into a variable another pointer p2.
42
00:07:05,040 --> 00:07:12,040
So this is not constant this is normal integer pointer this is constant integer pointer.
43
00:07:12,040 --> 00:07:23,040
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.
44
00:07:23,040 --> 00:07:30,040
So we think that we are modifying this value and setting it to 20.
45
00:07:30,040 --> 00:07:44,040
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.
46
00:07:44,040 --> 00:07:57,040
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.
47
00:07:57,040 --> 00:08:07,040
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.
48
00:08:07,040 --> 00:08:11,040
So that's why it's called undefined behavior.
49
00:08:11,040 --> 00:08:19,040
So now let's see it in the code and then you will see that it's not what we are expecting.
50
00:08:19,040 --> 00:08:41,040
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.
51
00:08:41,040 --> 00:09:10,040
And then finally we were setting this address value to 20 and then let's print out a and also print *p2.
52
00:09:10,040 --> 00:09:27,040
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.
53
00:09:27,040 --> 00:09:46,040
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.
54
00:09:46,040 --> 00:10:00,040
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.
55
00:10:00,040 --> 00:10:04,040
So both are same pointing to the same address in memory.
56
00:10:04,040 --> 00:10:16,040
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.
57
00:10:16,040 --> 00:10:19,040
How can the same memory address be storing two values.
58
00:10:19,040 --> 00:10:46,040
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.
59
00:10:46,040 --> 00:10:51,040
So in fact the compiler is doing this during compilation.
60
00:10:51,040 --> 00:10:57,040
So we are not printing the value stored at this address but we are just printing 10.
61
00:10:57,040 --> 00:11:01,040
So even after modifying it it's still printing 10.
62
00:11:01,040 --> 00:11:14,040
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.
63
00:11:14,040 --> 00:11:29,040
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.
64
00:11:29,040 --> 00:11:38,040
Now the last point is that const cast cannot cast to a type which is different from the original object type.
65
00:11:38,040 --> 00:12:04,040
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 *.
66
00:12:04,040 --> 00:12:08,040
So this will give the error the compile time itself.
67
00:12:08,040 --> 00:12:12,040
So that's all for const cast. See you in the next video. Thank you.
9556
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.