What is Dynamic Casting in C++ | Top and Latest 2021

Dynamic casting

Dynamic casting that’s what we’re going to be talking about today you guys don’t know what it’s costing is in general what’s that going to be here about that so definitely check that out and familiarize yourself with that before you move on to this Article if you don’t familiar with the type system in general I have a few Articles about that as well because costing is to do with types costing is a way for us to combat between the types that we use in several spots so you need to make sure that you kind of understand how the type system works and also maybe the point that the type system isn’t something that is particularly in Faustina staple spotted something that kind of gives us as a way to protect our card but it’s not something that we have to necessarily stick to your use because. Dynamic casting

we can freely cost between types of any types like if we so choose but Donna costing is provided to assist some somewhat of like a safety not really for me wanted to a specific type of costing now I’ve gone through kind of C. C++ stock costs is to kind of wasted cost this is east all costs which is just when you write your new type in brackets before a variable but then there’s also disabled bus stop caustic static cost  make probably more Articles about that but dynamic cost is a C. C++ stock because it only works in people’s buses work in St and it doesn’t extra work to make sure that we don’t like the way that we actually costs right it’s not it’s an it’s actually a valid costs we are just cost between 2 types and whatever happens is not like that it actually doesn’t validation for us to ensure that cost is valid.  Dynamic casting Dynamic casting

Now whether you use on the cost is again totally up to you I want to give you some points to kind of I guess educate you to sense of whether or not you should be using that if that’s something you wanted to but most are done and the cost is it is quite it it’s a tool is more like a function it’s important to realize that it’s not just like a cost that is done at like compile time it’s evaluated at runtime and because of that it does have an associated runtime cost I might even make an Article in the future comparing the performance of his normal kind of static auspices of that dynamic cost because of course is a dynamic cost of extra work it does kind of come with a small performance cost and that’s normal. But what Democrats does is specifically it’s used for costs along the inheritance hierarchy not you don’t know what inheritance is in several spots what classes are in see C++ check out my Article on inheritance and classes and all that stuff because that will help you out but essentially what that means is if I have a class that is a sub class of another class and I want to eat the cost to the base type or from the base type 28 derives type that’s where I can actually use a dynamic cost right so let’s just say we haven’t entity we have an ANSI class which is like the classic the kind of all of our aunts he’s in our game and then. Dynamic casting Dynamic casting

We have like a plaque loss and maybe look at any cost and they both entities so they derive from the Anstey class right if I just want to cost a playoff object back to enhance your project that that’s easy I mean apply already has the type and see that that can be done implicitly not costing is necessary although if we wanted to we could still use dynamic cost to do it now the other way around is where things get a little bit more complicated Sir by the other way around I mean if we have an entity instance and we won through Costa to apply out. We have no way of knowing whether on supply right. Dynamic casting

it’s as far as we know it is an entity pointer right it’s an entity instances an instance of an entity object right Sir what is an ANSI object well it could be of type entity could just be Nancy could be neither player I enemy right could just be an NC but it also could be. Our or it could be an enemy that’s totally valid we might have made it a play we might have made an enemy or could be and see if you could be one of 3 types if we want to say that actually I’m going to cost us to play a. We don’t know if it is a player or not the compiler has to either believe us and if we’re wrong to say wasn’t enemy and we trying to access a daughter that is unique to play out walk a function that modifies out of this you need to plan a programmable crashing it’ll be like a disaster.  Dynamic casting Dynamic casting

So because of that dynamic cost is actually used to validate that so what happens is if we have and it’s the instance that is actually an enemy right but we constantly trying casa to apply using dynamic cost that cost will fail and that dynamic cost will return a null pointer moves R. 100 and then we can validate that’ll do whatever we want so basically we could use it to check to see if an object is a given type so in other words I can try and do it and then the cost on that and see object to convert it to play out and then check to see if that returns not with a sense now that it’s on a plat right this is all a lot of stuff is more easily finding cards let’s dive in take a look at a basic kind of set up in which this might be useful and how we can actually use dynamic costing okay so what does a blank Super bowl party here because I really wanted that country guys everything so if we do what I just described if we have like Nancy right if we have a playoff which is NC state public and see.  Dynamic casting Dynamic casting

And then we also have that enemy class that I talked about which is also going to be an issue right now in terms of the daughters of these will have. It is not even required for us to actually add any Dada several we want just to keep this brief I think because it’s not really worth going into that stuff but basically the idea is we have 3 different types that they probably filled with functions filled with data that is unique to that specific types how do I make an ANSI well wishes they wanna make the playoffs I can just type in play a play a new play out never this Article was going to be using role point is I make an Article in the future about dynamic costing with my point is but this it is kind of a kind of the original dynamic cost that is used with raw point sorry we have a plan right now the thing is this play out already has 2 types it has both ends to eat and play associated with it it’s actually even got like a type listing as well which might talk about later but it’s got the those 2 types and see and last so what I could do easily just change this and see right all I can make an entity 8 people clap right implicit cost. Dynamic casting Dynamic casting

However once I’ve done that if I want to go back to playoffs I can’t easily do that so I can type in playoffs Pete kolinski right that’s going to give me an error why because well I have to cost at the different types the reason is that this could have easily been an enemy you know what if I do NC easy wanna whatever a closing your enemy right what happens here what about what happens if I do eat one right we don’t it’s still in NC but easy to play out is it an enemy we don’t really know what is at this point Sir because of that system we have to actually reassure compiled by saying that Hey it is a plan now in this case the this is dangerous because the ones actually an enemy and so if we because it’ll play this will initially work and in fact if enemy has a function that like play also has and steals the house and we try and run that punctured it’ll probably well before we would even notice any issues with it but if we try and do something that is unique to planet enemy doesn’t have such as access that and Donna members that only play house and.  Dynamic casting Dynamic casting

No enemy since the underlying type is actually enemy out program will likely cross will do some really weird things that we really don’t want to do so because of that we can again serve using roll costs or even like a static class because you can still use you know static cost with this like about them that’s fine instead of that what we can do is swap this out for a dynamic costs right now we try to do that at this rate is going to tell us that it needs to have a poem of the class I because then across on the west apology class that’s how we do that well we basically need to have some kind of actual function table that actually tell us that it is in fact a poem of the class types that we can make this we just have a full void in our current name or something like this doesn’t really matter but the point is that will make this class heavy table and thus this stuff to override which means that is it’s a polymorphic type and we can use dynamic costing with it again this was a real ANSI class it would definitely have actual functions that this is just making it a little bit more realistic as well Sir. We can cost it right so we’ve got out he was a player or we can use the one which is that and actually an enemy so we could say actually playoff just to make this a little bit more Clear and then we get 6 I actually enemies.  Dynamic casting Dynamic casting

Right so if we try and get our act out enemy into a playoff this should fail right but if we try to combat this playoff to apply up that should work even though they both got them at the moment that both entities okay. Sir let’s take a look at what this card will do if we were to execute it after the break point here and hit F. 5 and of course need to make sure that my name’s variables different things okay so this cost should file right because this is actually an enemy and what’s going to constitute a plaster at 10 and you can see this retains all the cost and walk however if I try and do it here you can say that he wants is in fact a valid NC it successfully was able to cost it into a playoff because actually what a player and that is essentially all the dynamic cost us if the cost is valid then it returns the value that you want to play a pointer but if it is not valid because it’s not a given type that you’ve kind of claim that it is then it will just give you now that’s what dynamic cost us in a nutshell now the question that you should have from this is but how does it nor . Dynamic casting Dynamic casting

I haven’t written anything and it’s not like super slow system manages language like C. sharp or Java or anything like that how does it nor that play up is actually a plan like how does it know this and see that I have is actually a playoff I’m not an enemy what the weather does that is it actually stores runtime type information especially when it’s cold RTI runtime type information is stored runtime type information about all about types this does Adam or they had bought the lets you do things Dynamic casting Dynamic casting like dynamic costing Sir it does 2 things to consider here first of all I. TI as overhead because suddenly types type types need to store more information about themselves then well otherwise present and day or 2 dynamic cost is also takes time because we need to actually check to see does the type information match is this S. T. actually an enemy or is it a plaza what type is that we have to do that allegation runtime anytime we going to cost so because of that. It does add up ahead now what we can actually do you know coach here is 10 runtime type information off if we don’t need it so we got our properties and different every compiler but over here inside safety both Boston language and visual studio or you can say we have this enable runtime type information. Dynamic casting Dynamic casting

If we were to switch that off and then we attempted to compile our card you would say that it actually would give us a warning which says that any cost you some politic type entity with this just means that we’ve switched off runtime that invention so with that with IT I also gives us unpredictable behavior and if we try and run this card and we hit F. 10 to get process invalid dynamic cost you can see we actually get an access violation sort that actually present era for us all right Sir it doesn’t give us now because the con because it doesn’t have that type of information so I make sure that you are aware of the kind of implications that dynamic because actually because they do kind of do extra things and they do require that artists guy is on in most cases if we watch you like for example do across from plants and see which is in place anyway it would be fine it wouldn’t crash but if we try and do something like this. Dynamic casting Dynamic casting

you can see it just it just crashes doesn’t even last a what adult and having Odyssey I own is pretty typical but also does add a bit of an overhead so just keep that in mind one last thing I want to just want to show you is that because of that I mean because we have dynamic Costner we can do that allegation that I mentioned we can do something like if he’s there now this is very similar to manage languages like C. Java well we can basically try and check what something actually is service actually enemy in our in like C. shop we could do if actually enemy is a playoff then we can do a lot in Java we have instance off sorry by using this kind of dynamic costing we can basically achieved the same thing we can say.  Dynamic casting Dynamic casting

Also See

What are ENUMS in C++ | Top Latest 2021

Leave a Comment

Your email address will not be published. Required fields are marked *