YouTube Video Transcript

2,378 words

Full Transcript

[Music] hello there this is rotation you're watching CBP nerds video series on C++ and in this video we'll be learning about static cast so in previous video we saw that how do you use cons cast and we have to use everywhere we should not use that and in this video we'll be learning about steady cast and I have almost 5 to 6 points here to discuss so let's start it so this is the first demonstration program here I need to uncomment that and it tells that it performs implicit conversion between types so implicit conversion is like what generally C++ or C compiler does when you're typecasting integer the point I mean integer to float and float to integer and interior to character and character to integer ok so those things comes over implicit conversions and we should always use static cast in order to do that ok so I have given this normal implicit cast and this is our static cast both are doing the same thing but let's suppose your program is failing somewhere and you want to check where am i doing all those implicit cast so searching this lining whole bunch of code is really very hard so that's why we should use a static cast in that case so you can just search it ok however I suppose we'll be doing the same thing but this is explicit message that I am using static cast and static means this typecasting will happen at compile time it won't go at runtime ok so it will check whether I can type cast F which is a float point into a which is of interior pod okay and I have given this comment you read it out ok so this was the first point we should use static cast ok let's go for the second point and second point tells that uses static when conversion between type is provided through either conversion operator or conversion constructor so we will see that here if you don't know what is conversion operator and converts the constructor we'll learn here okay so we having this integer class this class is taking integer as a value this class has integer X as a data member and we have this object here these two lines are very important okay here we are using a conversion operator see you're initializing object of type integer inside string how is it possible both are not equal right but still if you will compile this code it will compile without any problem okay let's check that see it has compiled without any problem and why this is happening because we have this conversion operator here okay we are initializing into string that's why we are having conversion operator for string if you want to initialize your class object to something else then string you have to provide the conversion operator for that as well okay so object is being initialized to string datatype okay so this function is being called we are typecasting X into string and passing and that X is getting and it slides into this one and this is also a weird thing this is talking about the concept called convergent constructor the rule of conversion operators sorry conversion constructor tells that if your class has any constructor with 1 parameter and you are initializing the same data type parameter into the object we will call that constructor okay so let's print the message here like what is getting called at what time so this is conversion constructor and next one is here this is conversion operator okay so we will compile this and we'll check what is being called it what time see conversion operator conversion constructor so the first thing is conversion operator okay so this was called I told you sorry this one okay but while we are discussing everything here the reason it is we know what will be called at what time here but here if somebody is looking at your code they won't be able to understand it much because they don't know what type of casting is happening and this is a compile-time casting so you should always use static casts for that okay so this first expression should look like this one and this second expression should look like this one okay ultimately this assignment is also doing the same thing what we are writing here but we should explicitly write that okay so the reviewer or somebody else is looking at your code after ten years they will be able to know or even in debugging phase it is really very important if you want to find where all those static casts are happening okay so here also we should use static cast both the cases converts an operator and conversion constructor okay so this was our second point let's move to the third point third point else static cast is more restrictive than c-style cast and let's see why I am telling like that because character pointer to integer pointer is allowed in C but not with static cast and this is really very dangerous cast if you are casting character pointer into integer pointer it means that you have a character pointer which is pointing to one byte memory location and your typecasting that one byte memory location to four byte memory location now if you will use this to write into memory it will write four bytes so that's the example here see we have a character C typecasted that address into integer pointer and you are updating that integer pointer which is a four byte with five so what it will do will see this diagram here you have memory like this and you have somewhere this address of C which is one byte okay now what you are doing your typecasting that address into integer Poynter so what this will do it is like now you are pointing to the same memory location but now you are having four bytes into your consideration so if you will use this pointer which is this P pointer and initialize five what compiler will do compiler will initialize five on all these four bytes so it's like it will initialize 0 0 0 0 0 1 0 1 like this and what you are doing here is you was allowed for this particular block but with the help of typecasting which is this one you you got access to the nearest bytes also which was not allowed for you so in this case you might corrupt your memory you can go to segmentation for lots of things are possible but that's not the point in it the point is here c-style casting this one allows this you should be getting error for this right but no your c-style casting is allowing this but if you will use C++ style casting this is not going to allow that okay these two things are exactly equal I mean your typecasting character pointer here two integer pointer and the same thing is happening here your typecasting character pointer into integer pointer okay but this is C style casting this is C++ s style casting and this won't allow I mean this won't compile whereas this will successfully compiled see I have given the comment here pass at compile time but fail at runtime I mean this might fail or might not fail it depends what address it is accessing is it allowed to access and all that but this is a danger thing and this is telling like it will fail compile time only okay because both pointers are not compatible pointer types okay so C++ cast avoid so many dangerous casting what see allows okay so this was the third point you will notice that this part was compiled see there is no error the error is here invalid is to be cast from character pointer to integer pointer this one but this one has compiled successfully so if you comment this part like this and we will go again and compile that see it has compiled successfully okay whereas this is a big crowd so you got the proof now let's look at the fourth point fourth point else it won't allow to typecast your derived into private base pointer okay I mean if you are using static cast it won't allow but if you are using again the c-style casting it will allow and that is dangerous I mean that is not allowed qey cast but still using c-style casting you can achieve that this is your derived class you have inherited base as private okay so let's compile this and you will get therefore that sea bass is inaccessible base of denied okay because you are having private here if you will write public let's write public and recompile this see compiled it no problem at all so if you will write private here this won't allow your code to compile so this was the fourth point let's look at the fifth point I hope you are not tired of looking points here so fifth point tells that use for all up cast but never use for confused downcast so we will see what do I mean by that so here in this code we have this base class we are initializing base into derived one and derived two so this is something like this base this is d1 and this is d2 so we have these two objects the red one and derive two now you are typecasting derived one object into base B base pointer one and same thing derive to address into base pointer to so this is allowed because you are going from here to here and in second case you are going from here to here so we know that we can typecast it like this okay so this is also perfect code this is also perfect cover but the problem lies here what you are trying to do is after typecasting into upcast like this you are again coming back but while you are coming back from here to here you are just switching so base pointer to which was holding derived to is initialize to derived one pointer okay see derived one so now from here to your again coming back like this this now here you are initializing d2 and here your name is in d1 okay so d2 is in each lies inside d1 type and d1 is in sliding to d2 type here okay and this is compiling so this point is not about where you should use static cast this point is about where you should not use static cast you should not use static cast if you don't know what your pointer is holding so you might typecast it wrongly okay so that's right or never use for confused downcast if you would compile this this will compile successfully okay see and this is the problem you can see that so this should not happen and you should use dynamic comes for that and I will give a separate lecture on that okay so this was fifth point let's look at the sixth point and maybe that is the last point yeah I don't worry you can go home no problem and sixth point tells that you should use study cast whenever you are casting to void pointer or from void pointer so let's see what is that so you got this I is equal to ten your typecasting address of I into void pointer then you should use study cast if you are typecasting any void pointer to some another pointer like this then also you should use static cast okay why because there is a predefined rule for converting anything into a white pointer and that knows how to convert any pointer to void pointer okay because this is general pointer so there is a standard way of doing this and this is obviously a compile time check so you should use steady cast okay so this is also a point here and maybe I don't have any point so let's look at the bottom lines bottom line says that for compatible type conversion such as flow to integer so you should use static for this purpose we saw this in first point second point is for conversion operator and conversion constructor we saw that in second point to avoid unrelated pointer conversion we saw this in third point where we will typecasting character pointing into India binder okay and fourth is avoid did I - private base pointer conversion we saw this as well and use for all up cars but never use for confused iron cast because there is no runtime check performed for the static cast conversion because static means it will be happening at compile time there is no runtime check here okay wrench and check happens with dynamic casts we will see that in upcoming videos and by the time you are watching this video maybe it is already there so check out the description field and 6-point is in tensor are more clear and see blissful style cast and 60s finding is easy like if you want to find where you are doing all static cast you can just easily find that but in case of c style casting it is nearly impossible until unless you are using some kind of regular expression or something to find that and maybe you won't be able to find that and it is error finding at compile yeah if you are using static cast it means you want to check everything at compile time itself okay so generally experience people tell that this is the first cast you should always go for if it is not compiling then think twice and go for other cast okay so I think we are done here so let's look at the next video which is maybe about trained to treat cast or dynamic cast I don't know you'll get the link in the description field I'll see in the next video I

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

YouTube Video HlNVgmvX1EI Transcript | YouTubeTranscriptFree