CPPB10 dynamic_cast | reinterpret_cast | const_cast | static_cast | Typecasting Operators in C++

Ramjilal4You8,109 words

Full Transcript

Hi everyone, I am your friend Ram and today's topic is casting operators in c++ programming language. In the last video, we had seen in detail about type casting or type conversion. In that I had told that There are two types of type casting, implicit and explicit. In implicit, you do not give any instruction to the compiler. It is automatic. In explicit, the programmer gives instructions to the compiler to convert the particular type into destination type by type casting. Right then an explicit type casting. There used to be two types of casting in C style type casting in which the destination type in its bracket was given as data type and it was assigned to the new data type by right and the second type was in explicit. Through the casting operator and what were the casting operators in that casting operator? Static cast, dynamic cast, cons cast and reinterpret cast. So today these four casting operators are static cast, dynamic cast, const cast and reinterpret cast. These are all four types in detail. I am going to discuss it with live code examples, so you are going to watch the video till the end. In this, you are going to see Up Casting and Down Casting, I am going to tell you what is it, how it is done and also volatile keywords. If you are going to watch the video, then you are going to watch the video. Watch till the end so that you can know about the Casting Operators because this is basically asked in the interviews. Generally one or the other is asked in the interview. In all the product based companies of every company, these Casting Operators are asked. So you watch the video carefully. Okay, so let's start the topic Casting operators. So casting operators are used for type casting in c + PS. I told you that it is used for type casting. If converting any type of data type. If yes, then in that case we use casting operators and these are of four types: Static Cast, Dynamic Cast, Const Cast and Reinterpret Cast. We are going to see these in detail one by one. Now before that we will understand because two terminologies will be useful in this. One is that in the case of dynamic cast, up casting and down casting will be used and in the case of const cast, volatile keyword will be used in this. Right, so here let us first understand them and then later we will move to our code, so downcasting. And what is up casting, so basically understand here that you have two, this is used in the case of inheritance that you have a base class and after inheriting from there, you are type casting a driver in the driver, then that is done. Down casting because what is base is up is right and if the derivative is going from up to down i.e. going from base to child then what will happen in that case is down casting but if you are doing derivative then in that case What will happen up casting will be correct So you have understood that if you are going down from your base then what is the volatile keyword of the derivative, it is a reserved word in c+p programming and what you do through it is to instruct the compiler. Let's do this, it is used to create identification with any variable. Right, here we used volatile. Now what volatile does is that it instructs the compiler that this particular variable can change at any time in the program. So don't optimize it, right don't do compiler optimization for that variable, it instructs the compiler, otherwise what happens if you do not use your volatile keyword here, then what happens in that case, the compiler will see that okay, this constant. If it is constant, it means that it is not going to change through out the program. It assumes this and what it does at compile time is that wherever the compiler looks at the local variable, it replaces it with its value . It will replace it with 10. If you do not use volatile and run it in the optimization mode, then what will happen in that case is that if you look at this program here, then here the pointer is PTR, right and through PTR. Here you are storing the address of what is local in PTR, this is not allowed in c+ because in c+ plus it will see what is the good local and it is constant so here there should be constant pointer, it will give compile time error but in c plus c. This is allowed in C programming. There is also this loop hole in C programming that The value of constant variable is changed through pointer, they can write it like this, so this is what we did here on line number seven, we printed F here and the initial value i.e. the initial value of local. They are printing it, after that we are changing its value through the pointer, right because the PTR is pointing to the local address, so here we are changing it, but if If you have not used this volatile then what will it do that the Kapal compiler will optimize it and wherever it sees the local, 10 will be printed there right? So see the difference here that if volatile has not been used then this line number nine will also print 10 and Line number 11 will also print 10, I will not run it, you will see it from here and run it in your compiler and run it with this optimization mode on, then you will see the difference and in the other case, you will use your volatile keyword in that. What will happen in the case, it will not be optimized and from here line number nine to 10 will be printed and line number 11 to 100 will be printed, so this is its difference, so wherever volatile comes or someone asks about volatile, the straight answer is that it is volatile . keyword tells the compiler to do not apply the optimization on this variable where we have used volatile keyword The volatile keyword is intended to prevent the compiler from applying on objects that can change in ways that cannot be determined by the compiler Correct this Prevents it from applying. So now you have understood what is up casting and down casting and where and how volatile keyword is used. Now let's talk about casting operators. So first of all we will see. First of all, let's talk about static cast. What is static cast, so the most commonly used casting in operator in c + p programming is static cast. You will find it generally used anywhere because it is very generic and generally This is used very commonly in programming, so it performs the compile time type conversion. It does type to type conversion, that is, it converts the type at compile time itself and gives it. It does not wait till run time. Mainly used for explicit Conversions that are considered safe by the compiler Right: The compiler converts whatever it thinks is safe. Use to convert between related types only: What does it mean? What is the condition that the type that is being converted should be numeric only? Like right is integer to boolean, follow is integer to float, double is double to integer, it should be like this or points in the same inheritance here, same inheritance here means that the related class right has some relation in it like child has. And there is a parent class and there is a child class so we can convert from parent to child and we can convert from child to parent right and from parent to child so this is useful here this correct provides a lot of up casting and down casting down casting and Casting is explained because you can convert your parent's pointer to child and child's pointer to parent but here they should have relation right two classes must be related higher kickly right so base class must be accessible to use a static cast . So this means that the base class here should not be private or protected, when we will use it from our base to base to child, the base class should inherit it publicly in inheritance and it should be accessible in the same case. Otherwise it will give compile time error, I will show you by running its example, now allow casting from any pointer type to void pointer and vice versa. You can convert any pointer into your void pointer through static cast and convert a void pointer. If you can convert Apn into any other pointer, then it also does the same, so these are its applications . Let us see it one by one. First of all, let us see the simple example. What to do in it, here is the name for Apn numeric. Here integer is right and integer is type cast in double. Using static cast, I told that it also supports numeric, whatever is numeric, integer to double, integer to float, float to double, all these. It is also supported here, but if in this case, we will do it like this, if we do pointer to character which is integer, then it will not allow, it will give compare time error, it is only for numeric, so I have used this. And to verify its type cast, I took the type ID and printed its name there i.e. which type it is, so I run it and show it to you along with see here that you Line number 29: Here I have printed the type ID name of Jo Nam Hai, so what is its type? If it is Integer, then int is being printed here. Correct. After that, I have converted Jo Nam Hai into double through static cast. Right and from here its name is being printed, so double is being printed here and from here, we have printed the name double which we have already type casted on line number 26. We have printed the name double here as well. So here it is also getting double. Print Correct. Here, we have confirmed that we can convert numerical value also through static cast. Now let us see the second example, what we can do in it. Here I told you that it should be numeric and pointer to related classes, now here it will not allow integer to character, it does allow it in C style casting. Here, look at line number 40, what are you doing on line number 40, what is the character pointer, right? What is the character variable, what is its address, what is the pointer , it is correct, so what are you doing with the character pointer here? If you are trying to convert integer into pointer then here this is allowed in C style type casting. Right explicitly you are type casting it but this is not allowed through static cast, there it will give compile time error and error. What will it give that it is not related right see here this error is coming invalid type conversion and this will tell at the compile time itself I run all of them together it will be a little easier to explain later this is here See, you have seen that a character pointer to an integer pointer is not allowed. Now let us see that for the class, here I wrote an integer class and in the integer class I took an integer variable. Now what from here? Here we will write its constructor and we have overloaded the conversion operator which converts, right. Here, I have used the string operator, right. String is a class, but here we have added it to the class. If you want to convert it, then you see here, here I have added this conversion operator, here I have added this constructor, now I will explain to you what I did here, first of all, I created an integer object on line number 67. And right to pass three values ​​in it because what we have in the constructor is a default argument, there is a parameter, we have put zero in it by default, so here even if we do not give a value, it will still be created as an object with zero right but our If you pass three, then path will come here, right here, OBG, if you have created it, then what will happen here, the constructor will be called, first of all, right here, look at the constructor call, first of all, what happened after that, your object here. Whatever we have created, we are converting it into string by explicitly type casting it into write string. Now here, if we are converting it into string, then the conversion operator will be called in the string. Here first right obj will be converted first and then it will be converted here. If it happens, then the conversion operator will be called, the second call will be that conversion operator correct, after that here line number 69 is 20, what is Rat, now 20 is an integer, or this one is not an integer, right class, this is simple int, right now. What will be converted from int first? Sorry, it will not be converted. This will first become an object of integer here because it will be similar to this type. Look here, the first thing that will be converted into this is the object of integer. Now there is a conversion operator for that, otherwise what will it do, it will create its own temporary object of integer, so here the constructor will be called with 20 value right, so here the constructor is called, see this, the constructor is called, correct after that. We will come to line number 72 and here we will see that OBG is already created with us. Now what are we converting it into string through static cast. So here the conversion operator will be called right through static cast. Conversion operator is being called. Here simply string the name of the class given by you. Through that the conversion operator is being called and after that here again an object of 30 will be created of a right integer and the value of 30 will go into it right then line number 73 this and this one are the same right so these are different. There are methods, one is that here we give this inside the parentheses, we have that integer class, and here we are calling our constructor through static cast, right, so here the constructor is called and before that the conversion operator is called, line number. For 72, now you have understood this, after that let us see our second example in which we will take two classes, one class will be base, one class is derived from base class, publicly written, if it is private then in that case also we will write these. You cannot use static cast because its condition is that the base class which is the parent class should be accessible. This is correct for the child class. So from here we are going to see up casting and down casting. So here I have derived. I converted the pointer of that d1 into base, from here it is C style casting and we can achieve the same using static cast. Here we have d1 and we are converting it into base pointer, so this is done. Up casting because we are going from the drive to our base and now we are doing the reverse, from here we are going from the base pointer to our deriver, so this can also be done through C style casting and the same can be achieved using The static cast edge is well right, we are doing this through static cast, so this is done, down casting is correct, so down casting is done through C style, up casting is through C style, and down casting and up casting is through static cast. There is this bonfire here but I will make it private and show it to you. Here I made it private. As soon as I made it private, see here, there is no problem in C style casting. C style casting is going on here but in c+p where static cast is done. Here it is giving this error and see here what is the error that conversion to this accessible base class is not allowed right because the base class is not accessible here so if this error is given then it must be public. Right, you have seen up casting and down casting. Now let us see that void pointer to any pointer or any pointer to void pointer, this also allows static cast, so see here, I have created an integer right. Now this is Took the address of here so this is i pointer i.e. we are converting the integer pointer to our void pointer using static cast so here this void pointer has come into v correct now void pointer this is integer pointer to void pointer now Our void pointer that we created, right this one v, now what will we do with v, convert it into integer pointer, correct here, so here, void pointer to integer pointer, this is also allowed, right, so any pointer to void pointer and wide pointer to You can convert it to any pointer and later print its value in your integer pointer. Correct, look here, this 10 is printed, right from the line number 100, so all these are left in it, so let's talk about it now. Dynamic cast is mainly used for safe downcasting at run time, static cast is at compile time, dynamic cast is at run time and its main use is to do the safe down casting right i.e. of the same type. There must be one virtual function in the base class to use dynamic dynamic cast. To use dynamic cast, the class must be polymorphic, that is, there must be at least one virtual function in it. Works only with polymorphous classes. Because it uses this information, that is, it is talking about this pointer, it is to decide safe downcasting right because it should be a virtual function or polymorphous class, I told you because what it does is that when it downcasts it, at that time that pointer What type information does it use? Write at the time of downcasting. Downcasting cast. Dynamic cast has run time overhead because it checks object type at run time. Using the RTTI. What is RTTI? Run time type information. Write run time type information. It provides information about the type of any object at run time. It is a mechanism that exposes information about an object's data type. Right, this information about the data type of that object is provided at run time. Time and is available only for the classes which have at least one virtual function right i.e. this one virtual function should be in the class i.e. that is the class it should be polymorphous in the same class this RTTI is used right i.e. it allows the type of An object to be determined during the program execution i.e. at run time. This allows the compiler to know the type of that object. So let's look at its example. First of all, what we did was that we took a base class and added a Pure a sorry not pure virtual virtual function, I used print write here and took it to the derivative, I also created the object of the derivative from the base itself and cast its pointer as my type, so in the base, this is Upcasting is done, there is no issue in upcasting, correct, but here, if you have upcasted it from here, BP is right, what is there in BP that the address is a derivative, right, your base is a derivative in it, correct, what are you doing now? Through dynamic cast , we are deriving the pointer which was there in BP i.e. base pointer. Now using dynamic cast here, it will be converted to Kisem convert derivative to but here the dp2 which is derived here is there. The null value will come because what this dynamic cast will do is that it will check at run time whether the address of the pointer in BP is its base pointer, it will see that it is from Driver One and convert it right into Driver Two. That is, both are not the same, the driver and ride two are different, so here, through the run time type information, it will extract the information that this is not the same type, then it will contain null value, it will not convert it, that write will add null value in it . It will not give any error, it just has null value in it, then we will check its null value here and we will print null here, but if we convert the base pointer of our bean into our bean type i.e. in the derivative, then we will print it here . Will give the correct value or will return it by type casting and there will be no null here, whatever actual data will remain here, the address of the pointer will remain correct here, so I will run it and show you, so this is the best example of this. And when asked in an interview about Dynamic Cast, the same question is asked, i.e. why should it be used, what are its advantages, so they use it for safe downcasting, see here 'null' is printed and here ' null' is printed. Null is printed, then you are trying to convert it to another type and this actual value which is not null i.e. whatever value is present in the pointer will not come i.e. proper down casting will happen in that case. Where the type is same and the type information is taken by the compiler through RTTI at run time right here. This is doing the safe downcasting at run time using the RTTI information. So this is done for downcasting. Now let me show you if here You will not use your virtual function either see here you will get this error As soon as I removed this and see here the compile time error is showing that the operand of a run time dynamic cast must have a polymorphous diem a polymorphous class type Look right here, it must have a polymorphic class type so it will give compile time error here. If you have to use down dynamic cast then the class must be polymorphic i.e. it must have a virtual function. Correct as you see here. If you use the virtual function again, then that compile time error goes away, no error, so the condition of dynamic cast is that you must have a virtual function in the class, that is, the class should be polymorphic and in the case of down casting, if it is If you have to do casting then there is no need to use it because what it does is that it is a little slow compared to static cast because it has extra overhead right here Dynamic cast has run time overhead because it checks object type at run. Time using the RTTI, when you have to downcast, you have to use it at the same time. If you have to do upcasting, then in that case you can use static cast. There is no issue right and that should be used only because it is a little slower. Then come static cast. Now let's talk about const cast. So what is const cast that it is used to cast away the closeness and volatiles of variable volatiles. I told you that when volatile keyword is used then the compiler uses it. To It says that you do not have to optimize it, right then it becomes volatile and constant, meaning if you use const keyword with any variable then it tells the compiler that its value is not going to change through out the program, so in If you want to remove the properties of both, if you want to overcome them, then what will you do? Will you cast it away? How will you do it using cast cast right? So see here, one thing has become undefined, its applications are that this is its own. Let's see one by one here, first of all, if you had seen there in c+plus, when I had shown that example of volatile one, you can change the value of constant variable through your pointer, right and that proper value will change. But in c+ p it will give undefined behavior right and it is safe casting what happens if the tie top of the cast is not same as original type and this const cast is also safe such that it should be of same type other wise This will also give a compile time error, right can be used to pass const data to a function that does not receive const, that is, whatever you assume is in your function definition, what is there that the function is accepting non-constant variables but your If you pass a constant variable then the compile time method will be given, but what will you do? Here you will use const cast and remove its constant and then pass the variable as an argument, it will work properly, that is acceptable use. To change non const const class members inside a const member functions, this is a class, there is a member function in the class and the member is a variable, the member function is constant, right, so as soon as the member function is constant, then the this pointer that comes in it will become constant. Through it, its member variable which is a non-constant variable cannot be changed because of all the variables in it, which means it is a const function, through it we cannot change any of its variables, but here this pointer is called const. Let's make it non-constant by casting type through it and then change the member variable through it. Now let's see their example. First of all, we have to remove simple volatile and const. So here we have taken a variable pointer . And what is const and volatile, write is also const and volatile, both are used together, now its type ID and all these will run on their own, if you look here then the one which comes first in the starting will tell both const and volatile but When you cast its const, then what will you do here? Through cast cast, its const and volatile rights will be removed here. If I run it and show it to you, then I will run it together. How to use it all? What is this, it will be a little easy in explanation, so here let us see first of all, the first one is our use case, so what are we doing in this, our simple is this pointer taken as consu and volatile pointer is right integer pointer and use of consu late keyword in it. What did you do? Look at its type name here. The name of the type ID is type ID of b1 right and ID of b1. What is Integer const volatile pointer right? Integer is also Integer which is const and volatile is right but after that. What did you do here on line number 29? Here you cast a const and converted it into an integer pointer , which is a simple integer pointer and not a constant, neither a constant nor a volatile one. Write it in this. Converted and now we will see its type ID and name of c1, right of that type ID, what will be the name of type ID, only simple write, type ID of c1 is integer, pointer, no const, no volatile, correct, so this confirms it . That you can remove both your volatile bus and const through const cast, you can also run it in your compiler and see this is the type ID header file, here you have used it, see this type info here. There is this header file, it has to be included and after that you can use your type ID. After this, let's see which cast is safe or not in our second use case, then what do we do to make it safe. Pay safe means that it should be converted in similar data type only. See here, I took a1 of an integer and the address of a1, I wrote b1 in the const integer pointer and stored it in it. Now the b1 pointer is an integer pointer. Type here right b1, if you try to change it to your character then it will give compile time error. Will give a const cast can only adjust type qualifiers right i.e. it removes the type qualifiers of const volatile only . The work of removing constants or longness is not allowed, what we are doing here is trying to change its type right b1, what is integer pointer and to change it into character pointer here? If you are trying then this also will not allow it. Right, it is safe because you will convert the same type to the same type at the same time. This means it will be used in other wise or the compile time will give the third use case as its own. That is, in this we will try to pass the constant data to the function to receive the constant right here I have created a function named fun and in it it is taking a simple integer pointer, right here it is not taking a constant integer pointer, it is a simple integer. He is taking the pointer and what will he do from here, from here he has a constant, the pointer is a constant and he cannot pass it directly to him, what else will he do, he will give a compile time error, right, so see a value here. I took which int when I took a PTR, I created a pointer, I wrote which int pointer and now I will pass it directly to the PTR, here it will give a compile time error, see the red line from here because the argument is of type const int. JAR pointer is incompatible with parameter of type int JAR pointer right because what is the parameter here, int JAR pointer is simple but what is this here, what is PTR, what is const inj pointer, it is not compatible with parameter correct, then this What will we do? We will remove its const through our const cast and from here I did the const cast of PTR, simple made it into integer pointer and made it integer pointer. Now from here we can pass our PTR and from here, no error. Right see from here you are getting returned from here and its value is also being printed T 10 P 10 T E 20 20 is printed from here Now let's talk about another use case in that we will see that change member variable using const function. What is the member variable? Here we have the class. In the class, the member is private. The member variable is integer. This role is the role. We will include the role from here in the constructor and from here, what is the function, what is the member function, this is ours and this. Const function is a const function, so the member variables of the class cannot be changed through it. Correct, because what will happen inside it is that the this pointer will become constant and through the constant pointer it means that through the object itself it can be changed . If you can't do it, then what will you do here? As soon as you see it from here, you have created an object of Student A, how much initial value have you put in AA, have you put the initial value here, right through the constructor, this is the initial list. This is done through this list. I have set the role by three, after that we will call the function from here, we will print its value here, add get role, whatever is the value of our role in get role, it will return it and print it. After that, as soon as you call the add function add fun add f, what are you doing here, this is a pointer because it is a constant, so first of all we are casting its type, using cast const cast, making it simple by type casting. What is this also in Student Pointer? Conus is Student Pointer. Rat is Conus Student Pointer. We converted it into Student Pointer i.e. Simple Pointer and then put an arrow on it. Since it is a pointer, we will use the arrow and here the value of the role will be changed. We are changing it to five , that is, the value which was three has now been updated to five and from here we will print it. You can also do this through the const function which is a member variable's data. Apna can be updated using the const cast. See here how much is the old roll number which is three and what is the new roll number which is five. See here this is another use case. What are you doing in this. The constant value of apan is 10 and Made a constant pointer to store the right value because it cannot be stored in a non-constant because it will give a compile time error. See here that a value of type const integer cannot be used to indicate that it is an entity. Off type integer pointer write is allowed in C but it is not allowed in C Plus to write a constant variable. Apn has to be stored in constant pointer only, it is not allowed in non-constant pointer, so made it a constant pointer from here, but now what is the way to create that loop hole in C so that the value of Apn constant variable can also be changed? Used to give using pointer, but what to do here, you will have to make the pointer constant, otherwise it will give error there, then what happened to it that you can use const to convert the pointer which is a constant pointer into a non-constant pointer. Now from here you have used the constant const and converted it to non-constant. Now from here , if you try to change its value through your non-constant pointer, then it will give an undefined behavior , how will it give because see here in this It can also be right, it is not advisable to do so, I am just telling you that if you do not want to do this, then here the pointer PTR one which is a non-constant pointer, right, you have converted PTR, through that you can change its value. If we try, here we made it 20, now we will print its value here, so basically what should be the PTR and to whom it is pointing, which is the location of the well, right, which is the variable well, it should be updated but not like this. Will be right here so here is the value of PTR and PTR and PTR and this is 20 will be printed and well still 10 will be printed here see pointer PTR is 20 pointer PTR 1 is 20 and well kitna is 10 right this one so this is undefined Isn't it because where is it actually storing the temporary value and printing it from there and not the actual value that is being updated, it is not getting updated, so this is also an undefined behavior. In your compiler can show some more value but it is not defined right it is not supported so that's it now we will talk about const cast now reinterpret cast so reinterpret cast is basically used to convert a pointer of some data type. Into a pointer of another data type: Now here I am saying same data type to another data type, meaning it is not necessary to have a relation in it, it is not necessary to be of the same type, be it a pointer or any type, convert it directly into another pointer type. If you want to do this then in that case you use your reinterpret cast. Write without thinking of any relation type and at least if the type data type before and after conversion are different write then in that case also one thing works the same. So it is good if Even if it is not there, it is used to convert right does not check if the pointer type and data type pointed by the pointer is the same and not right i.e. the first type is the source type and the target data type is the same. It doesn't care whether it is there or not, it reinterprets cast and what it does is that it converts it bit by bit. So, very special and dangerous type of casting is very special and dangerous because it supports all types. Any type or source will convert it into destination type. Right and dangerous because it is risky. Right, it is suggested to use it using proper data type. Pointer data type should be same as original data type. Right, this is to be used only when you have proper data type. Know that you should not use it the other way. Pointer data type should be the same as the original data type, that is, the target data type should be the same as the source data type. Only if you know, you should use it, not the other way. Should be done if we use this type of cast then it becomes a non-portable product right now so it is suggested not to use This concept becomes non-portable because what it does is that it lets the lower level know that it has its It is used when we want to work with bits. Right, I told you that it converts it bit by bit to the data in that memory location. If you have to work with bit wise, you have to access the memory at lower level, then in that case it should be used. I will also tell you a best example of this using the use case of structure. It is only used to Type cast any pointer to its original type. Let's see its use case. What did you do in this? It is a simple example that here you took a number as an integer and made the pointer to this number a pointer to this number. And from here we type casted the number pointer into this character pointer using Reinterpret Cast - This is the same use case I saw in the case of static cast and in that case, if I tried to convert integer pointer to character pointer through static cast, then an error came there because it is not a related type. Because numeric type supports static cast and or supports object points which are related class points, but what we are doing here is that we are converting it from integer to character pointer, so this is here. There will be no issue through interpret cast right here. From here we will print the address of the number which is our pointer. From here we will print the address of the integer pointer. What are we doing with this why the address of character pointer will come here then we will call it wide pointer. And then we will print its address from here, here I will run it and show you this one also, see here the integer address and the character address are the same because it is converting it bit by bit, now its structure. If we look at the following use case, what will we do in this, we will access the memory at the lower level, right member by member, here I created a structure, here in the right structure, I made its structure variable A and here again A through simple variable. Therefore , we are using dot as our correct, so here all its members are integer zar int zar character bullion, got the value assigned to all of them, from here now what are we doing after this, we are making a pointer to our s. Integer and it has been type casted. Right here, if we take the address of s in the integer pointer, then this pointer will become correct. Type casted it into our integer pointer, whereas what is its ta, what is my struct? Now my struct is separated from the pointer. Converted it to integer pointer, correct here, now what is P doing? Whom will P point to? Here it is pointing to its first member, its starting address, its address is being held, right now from here the pointer is P. Through our size of s we will do size of s, what is the size of their structure, what will be the size of the structure, our 4 byte for Its size is done, see from here, this is 12 bytes. Size of structure is 12 bytes. You can calculate its size through this size of operator. The right of any data type is my structure and what is its size. 12 bytes now here. What is the starting point of the structure right here? What will the pointer P do? If it is of four bytes, then here it is an integer and what is its type? If it is an integer pointer, right, then it will read the first four bytes. Because what is the size of an integer, it is four bytes, it will read four bytes, so what is kept in the first byte, there is only integer value which is five, right, so this five will be printed here, correct, now after that. What did I do, I made it p plus plus? Now we will do plus plus. When we do plus plus on the pointer, then what will happen if its size remains the same, it will go further, then it will go further byte further, right from here, further byte and further. So what is kept there, here the value of y is kept and after going further we will see whose type it is, if it is of integer type, then it will read four bytes in it, then as soon as it reads four bytes, it will write here. Here, how much is there in the byte, 10 is kept, so 10 will be printed here, correct. Now here you see that you are accessing the memory at your lower level, directly writing bit by bit, there are eight bits in each byte, which is written. Bit by bit speaking or byte by byte, whatever it is, right, in that case we are accessing memory at lower level here, bit by bit here, now we know that here now, after this now here is the pointer. If you know that character value is kept here then what will you do? Now convert this integer pointer into character pointer, right else wise will read it into integer, if we go further here then we will convert it into four bytes. It will read till the next step at once which will be inverted right here, now whatever your point is, we will convert it into character pointer here because we know that now here The value is character right so here through reinterpret cast we will convert it into character pointer and now we will print its value here as we have pointer. If you go here, you will see that the pointer is correct, now you will see the type of character here, if the pointer has that character, then only one byte has to be read, because what is the size of the character, it remains one byte, so here one byte will be read. That and what will come in one byte, a will be printed here, this right here a has been printed, now in one byte and after that we will do plus plus, if we did plus plus here, then why is the size of the character one, so go ahead by 1 byte. If it goes ahead by 1 byte, then it will get this boolean right there. So what will it do here? It knows that this is a boolean value, so the compiler does not know anything, why the compiler then moves ahead bit by bit in whatever block of memory it means. So here, then I type casted it into Boolean, from here I did plus plus and from here, through reinterpret cast, I converted the character pointer which was there into Boolean pointer, from here right so now points which are see pointer. is point to boolean value so it is required to access the value using the same type conversion right so we have used data type of bp pointer to boolean pointer right so here we have converted it to boolean pointer and through boolean pointer again You are accessing that value, so here you have accessed the memory at a lower level and in that case, one of its best uses is the right to reinterpret cast. You know which type to convert and there. From then the value has to be accessed in the memory and from here b p pointer here you see this a one has come right after a one correct because it is true so right now the third one is its use case is that suppose now What has happened is that whatever simple case has become a primary data type, it has become a primary data type as well as a user defined one because here we have converted the object pointer of my struct into an integer pointer. But see also in the case of classes, here we have created an unrelated class A, a separate class, class A, I have created B, which is a separate class, there is no inner tense in it and here we will convert their points into each other. See here, I have created a new class. Created B i.e. created a pointer of B class and created a pointer of A class right so from here you can see what is the Converted back to the pointer of B, that is, the point types of unrelated classes are unrelated classes, their object points can also be converted into each other, correct here, so this is its use, so this is what I saw, I ran it. It has been done and shown to you here: In class A, in class B, from here we are calling our function. What is new A in new A? The actual data of B is the address of the object of B, but still this Here the function is calling A because its type is A and it will be resolved at compile time because it is not polymorphous right it is not virtual and from here we are calling its function B correct So this is done Reinterpret Cast Thank you for watching the complete video Please don't forget to like the video and subscribe to the channel so that you will keep getting updated videos and tell us by commenting how you liked this video, see you in the next video till then keep Learning and Keep Exploring

Need a transcript for another video?

Get free YouTube transcripts with timestamps, translation, and download options.

Transcript content is sourced from YouTube's auto-generated captions or AI transcription. All video content belongs to the original creators. Terms of Service · DMCA Contact

CPPB10 dynamic_cast | reinterpret_cast | const_cast | st...