Dynamic Casting in C++

  Рет қаралды 119,564

The Cherno

The Cherno

Күн бұрын

Пікірлер: 146
@Nick-lx4fo
@Nick-lx4fo 4 жыл бұрын
"I think I might do a video on [side topic]" Just gotta wait 10 more years
@mehmetcanturkes7228
@mehmetcanturkes7228 4 жыл бұрын
I'm constantly amazed of how useful these series are. Thank you for creating these.
@GabeRundlett
@GabeRundlett 5 жыл бұрын
Keep up the C++ videos! It's awesome when you talk idiomatic c++ because it's really informational
@zraakuladann3946
@zraakuladann3946 5 жыл бұрын
Thanks for this video. What a councidence, lately ive been re-watching all your C++ playlist.
@dafdaf4052
@dafdaf4052 5 жыл бұрын
Your channel is amazing and very informative. Have you ever thought about making a 2-3 hour video, where you complete a project and explain it step by step? I have been using lots of resources, but yours is truly inspiring. Keep up the good work!
@Mono-ip2fx
@Mono-ip2fx 5 жыл бұрын
Finally c++ series continues!
@aleksandarfranc1094
@aleksandarfranc1094 5 жыл бұрын
Yeaaah Cherno is BACK with C++ series. Thanks. Keep on going bro.
@notsalman
@notsalman 5 жыл бұрын
Can you please do a video on C++20? Thanks. Also love your videos!
@supremedeity9003
@supremedeity9003 5 жыл бұрын
is c++20 already here?
@kenappleman5444
@kenappleman5444 5 жыл бұрын
Check out Jason Turner on KZbin he is a member of the c+-+ committee and does tons of c++20 videos
@climatechangedoesntbargain9140
@climatechangedoesntbargain9140 5 жыл бұрын
also checkout cppcon, they have been presenting c++20 for years.
@kenappleman5444
@kenappleman5444 5 жыл бұрын
@Peterolen no not 100% sure but he covers a boat load of c++ 20 features, it's good stuff
@notsalman
@notsalman 5 жыл бұрын
Ken Appleman Awesome! I’ll check it out. Thanks
@broly4988
@broly4988 5 жыл бұрын
I'm so glad you updated this series! The microphone is taking a bit to get used to but cheers!
@justpaulinka
@justpaulinka 5 жыл бұрын
So happy that you've finally starded making more videos about C++!
@fallingpizza11
@fallingpizza11 5 жыл бұрын
My prof sucks and makes learning C++ boring and a chore. After every lecture I'll always watch your video on the topic and instantly get it. So thanks!
@vanjamijatovic6332
@vanjamijatovic6332 5 жыл бұрын
Just never stop making videos! Ur awesome
@jean-fredericfontaine2695
@jean-fredericfontaine2695 Жыл бұрын
The speed at which i was able to get back to c++ because of you is staggering. thank you brother.
@DavidRodriguez-mx1nw
@DavidRodriguez-mx1nw 4 жыл бұрын
I'm studying c and c++ at 42 School and your videos are helping me out a lot. Thanks for all this content.
@1Dr490n
@1Dr490n Жыл бұрын
Kinda funny how people always speak about code optimization when talking about the fast-as-hell language C++ (like, how to decrease time by .00001µn), but writing reeaaally slow and often weird code when using python (optimal code: 3s, chosen code: 35s)
@aleksandarristoski2777
@aleksandarristoski2777 3 жыл бұрын
I just want you to know how much your work has helped me and you should be appreciated more. Great job!
@ThatOneBullet
@ThatOneBullet 5 жыл бұрын
Best game dev/c++ channel!
@sirenti9384
@sirenti9384 5 жыл бұрын
could you do a video about lvalue and rvalue references in C++. It would be pretty helpful
@klarnorbert
@klarnorbert 4 жыл бұрын
lvalue: values, that you can refer with a name(variable), like int a. Rvalues are which are not lvalues. int a = 5, 5 is rvalue. Rvalues only can appear at the right side of an assigment, you can't do this for example: 5 = int a;
@damnstupidoldidiot8776
@damnstupidoldidiot8776 4 жыл бұрын
@@klarnorbert An rvalue reference is an lvalue (or an xvalue, I forgot which).
@fruitgummy4935
@fruitgummy4935 4 жыл бұрын
I've been watching your videos for my cs projects and they are super super informative and straight-forward, thank you!
@zetadimensions
@zetadimensions 5 жыл бұрын
Very well explained! I hope you make more C++ videos as they are very useful.
@kocho4242
@kocho4242 5 жыл бұрын
dynamic_cast, when fails, doesn't always return nullptr. When we are working with references (not pointers) bad_cast exception is thrown instead.
@pooria_garrett3020
@pooria_garrett3020 5 жыл бұрын
Why would we even use reference with it to cause an exception?
@shaondawsoums3644
@shaondawsoums3644 5 жыл бұрын
Hey Cherno !! Thank you for all the work you put into these videos.. Can you do one on Exception Handling and how/where to use it.
@yellowcrescent
@yellowcrescent 4 жыл бұрын
Have always wondered how this worked-- have never heard of RTTI (for C++) before. A few months back I tried poking around UE4's Cast-related classes to see how they handled dynamic casting, but they use a ton of templates and their own type annotation system, which made it difficult to parse. Thanks!
@0x907
@0x907 5 жыл бұрын
HOLY SHITTTTTT THE LEGEND IS BACK WITH THE GOOD SHIT
@quadadulahma4297
@quadadulahma4297 5 жыл бұрын
Can you do a video on c++ variadic template functions? I really struggle on those.
@Narblo
@Narblo 5 жыл бұрын
I think he did one of those. There is a video where we work we variadic template funcitions. Logging system in game engine series i think
@bamberghh1691
@bamberghh1691 5 жыл бұрын
@@Narblo i still think that variadic templates need a separate video
@theRealSPACEDANCE
@theRealSPACEDANCE 5 жыл бұрын
@@bamberghh1691 It definitely needs its own video. Variadic templates are pretty rough at first, a lot of people had a hard time with understanding that part in the game engine series.
@alanmejia1330
@alanmejia1330 5 жыл бұрын
Thank god a new video 🙏🏽
@avtem
@avtem 5 жыл бұрын
Thanks for making your videos, I appreciate them so much!
@adamshield5029
@adamshield5029 5 жыл бұрын
Great stuff! I never appreciated the value of dynamic cast before but it can actually be super handy. It's like a visitor in variants only for polymorphism.
@furkannarin2844
@furkannarin2844 9 ай бұрын
So, I could create a project where every C style casting is correct and the program wont crash. interesting
@AnhNguyen-q8k
@AnhNguyen-q8k 4 ай бұрын
wait the like button actually lights up. like magic. it's my first time seeing such a thing
@KIPeR97eS
@KIPeR97eS 5 жыл бұрын
2 days ago i returned to this series, and you upload a new episode after 8 months lol
@worlddj1364
@worlddj1364 5 жыл бұрын
Welcome back man.
@eleganteatinginjapan659
@eleganteatinginjapan659 5 жыл бұрын
Would really appreciate a video on static_cast, const_cast, reinterpret_cast and dynamic_cast and the differences between them
@ultimatesoup
@ultimatesoup Жыл бұрын
Static: cast done at compile time, safe if you KNOW the type Const: add or remove const. Should never use this one. Const should never be removed and if you need to add const you can use std::as_const() instead. Dynamic: does a runtime check to make sure the cast is valid Reinterpret: completely changes the type, this is primarily used when interacting with old C APIs.
@ThibChap
@ThibChap 4 жыл бұрын
To just check the type, you can actually use the *typeid()* function, which I believe is faster. if (typeid(actuallyEnemy) == typeid(Player)) // ... It's roughly equivalent to typeof() and object.GetType() in C#. Great videos by the way!
@ultimatesoup
@ultimatesoup Жыл бұрын
I would not use typeid....it typically doesn't work the way you'd expect because it gives you the EXACT mangled types so pointer, ref, const, volatile all come along for the ride
@cesarnunez8802
@cesarnunez8802 2 жыл бұрын
Your videos are saving my life!
@BosakMaw
@BosakMaw 4 жыл бұрын
In 8:10 it's better to define a virtual destructor like you mentioned in previous video.
@danielphd5072
@danielphd5072 5 жыл бұрын
Thank you for this video about Dynamic Cast in C++ #cpp #cplusplus That was very informative
@firnekburg4990
@firnekburg4990 5 жыл бұрын
Thank you so much ! Best explanation ever.
@MadpolygonDEV
@MadpolygonDEV 3 жыл бұрын
Incredible useful video for game programing
@h.hristov
@h.hristov 5 жыл бұрын
Love u man. Can you do more modern c++ videos please - STL specifically
@baaryl
@baaryl 5 жыл бұрын
Finally we have new c++ video 😀
@UnWorld
@UnWorld 3 жыл бұрын
If you pause at any point, you can notice Cherno intensely gazing into your soul
@DDeathdealer007
@DDeathdealer007 5 жыл бұрын
It'd be cool to have suggestions about alternative coding patterns in the case that you want to avoid the overhead/cost of dynamic casting. Great video, thanks!
@smarthumanism3221
@smarthumanism3221 2 жыл бұрын
Thank you for the teaching!!! Great lectures!
@zjfjack
@zjfjack 4 жыл бұрын
Actually, I think we can use if (auto p0 = dynamic_cast(actuallyEnemy))
@mladen777
@mladen777 4 жыл бұрын
Does it throw an exception when encountering a dynamic cast with RTTI disabled, if in case of RTTI enabled, the dynamic cast is successful?
@MsJavaWolf
@MsJavaWolf 5 жыл бұрын
You should do the other casts next, this video will be more educational in the context of all types of casts.
5 жыл бұрын
Thanks Cherno!
@aquavitale3551
@aquavitale3551 5 жыл бұрын
Classic C++ series scenery and hairstyle!
@SimplySpiceIt
@SimplySpiceIt 5 жыл бұрын
Isn't that enough/ essential to provide a virtual destructor to the base class to achieve the polymorphism, rather than providing a virtual function(which may or may not have in a Base class)? In short, virtual ~Entity() = default; is essential than having a virtual member function in Entity at first place(IMHO).
@KishoreG2396
@KishoreG2396 5 жыл бұрын
Yes, you should always provide a virtual destructor for polymorphic base classes. I recommend just making all destructors virtual (unless the class is marked final) whether or not they are in inheritance just so you don't have to deal with the problem if you ever decide to inherit from that class. Of course, a virtual destructor will automatically make that class polymorphic and thus generate a vtable but there really is close to zero overhead with modern compilers.
@vinniciusrosa8284
@vinniciusrosa8284 4 жыл бұрын
My way of learning is not that fast. I have to watch a lot of times to really understand that, but... I liked you video! Thank you.
@buddhasarchive8385
@buddhasarchive8385 4 жыл бұрын
thanks. very clear to understand
@abhisheksa6635
@abhisheksa6635 Жыл бұрын
Nice explanation
@alexandre8350
@alexandre8350 3 жыл бұрын
Thanks
@MrTomro
@MrTomro 5 жыл бұрын
Amazing man, explained so well...
@YashPandey_the_emperor
@YashPandey_the_emperor 3 жыл бұрын
7:15 Imposter!
@mystmuffin3600
@mystmuffin3600 11 ай бұрын
Why does dynamic_cast require the base class to be polymorphic?
@bryanlozano8905
@bryanlozano8905 3 жыл бұрын
You are really good! Thanks!
@VasaMusic438
@VasaMusic438 5 жыл бұрын
Great !! A new c++ Video !! THANKSSSSS!!!!!
@ben963147
@ben963147 3 жыл бұрын
Thank you, helped me a lot
@zoriiginalx7544
@zoriiginalx7544 5 жыл бұрын
C++ series back :D
@MarcoGiordanoTD
@MarcoGiordanoTD 5 жыл бұрын
Isn't usually dynamic casting cosindered a code smell? Not for anything dynamic cast is doing, but mostly because you are breaking your polymorphism abstraction, which mostl likely means polymorphism might not be the right approach for the task? Of course as all things in programming is not an hard rule there are of course valid use cases.
@climatechangedoesntbargain9140
@climatechangedoesntbargain9140 5 жыл бұрын
consider a client knowing the type and passing it a function which operates on the super class. The function returns a object of the same type -> you know what the type is.
@MarcoGiordanoTD
@MarcoGiordanoTD 5 жыл бұрын
@@climatechangedoesntbargain9140 the thing is why do you need to know the type in the first place? if you do probably polymorphism might not be what you need? Sure you know the type you know is going to be safe and is going to succeed to cast. Can't what you are trying to do be achieved with a virtual call? If not then why using polymorphism? Possible other design can be envisioned.
@climatechangedoesntbargain9140
@climatechangedoesntbargain9140 5 жыл бұрын
​@@MarcoGiordanoTD e.g. the server needs me to implement a certain interface - but I still want to use the other methods (not in interface) after the server has processed my object. Because the server does not (need) to know the specific type, it also does not return a object of the specific type.
@climatechangedoesntbargain9140
@climatechangedoesntbargain9140 5 жыл бұрын
@@MarcoGiordanoTD what would be an alternative? given that runtime polymorphism is needed?
@MarcoGiordanoTD
@MarcoGiordanoTD 5 жыл бұрын
@@climatechangedoesntbargain9140 There are different methods than polymorphism, a more c like method might be to use opaque handles passed to a manager which keeps bookkeping of POD structs. You convert to the right struct and use functions on it. This avoids the whole vtable/inheritance going up and down the inheritance. This might not be ideal of course, as might not be polymorphism. Really depends on the use case, what I am trying to say is, lets just not start sprinkle dynamic cast everywhere but really consider what we are doing, dynamic cast usually is a "heavy duty" weapon :D
@caesarzheng3815
@caesarzheng3815 4 жыл бұрын
Beautiful..
@TheZenytram
@TheZenytram 3 жыл бұрын
for what it would need to be used ?
@DipsAndPushups
@DipsAndPushups Жыл бұрын
Why doesn't dynamic cast return an optional?
@seditt5146
@seditt5146 5 жыл бұрын
I was just asking this question last night on OLC discord asking about if I should or shouldn't do this in order to gather information about Collider* type passed into a resolver method. I still am not sure if I should go this route or not.
@comfypillow2131
@comfypillow2131 5 жыл бұрын
thanks for doing this.
@michael541
@michael541 Жыл бұрын
As I understand Dynamic cast is the similar as Cast in Blueprint. Am I wrong??
@vaggo9611
@vaggo9611 Жыл бұрын
🎯 Key Takeaways for quick navigation: 00:00 📚 Precompiled headers improve compilation speed significantly. 03:55 📦 Precompiled headers store commonly used header files in a binary format for faster compilation. 05:18 ❌ Avoid including frequently changing files in precompiled headers to prevent slow rebuilds. 06:44 🧩 Precompiled headers are useful for external dependencies and standard libraries. 09:03 📝 Including dependencies directly in source files can improve code modularity and readability. 11:46 💡 Precompiled headers are most beneficial for code you don't frequently modify. 14:13 🖥️ In Visual Studio, create a CPP file for the precompiled header and set it to create precompiled headers in properties. 17:44 ⏱️ Using precompiled headers reduces build time significantly, especially for larger projects. 18:52 🐧 In GCC, compile the precompiled header to a binary format and include it for faster compilation. 20:27 🤖 Precompiled headers should be used in nearly every software project for faster builds. Made with HARPA AI
@sergeykolesnik1171
@sergeykolesnik1171 5 жыл бұрын
Like in the previous videos, here you have mentioned the overhead when using dynamic_cast (contrary to static_cast). So, my question is: what would be the difference in perfomances if instead of dynamic_cast checking, you first validate the ID (interface ID, get it via overloaded pure virtual function like "virtual size_t IID() const = 0") and then use static_cast?
@master138
@master138 2 жыл бұрын
Is dynamic_cast function a part of iostream?
@0xlyptox396
@0xlyptox396 5 жыл бұрын
Hey, which Theme is that? It's really awesome :D
@cole-nyc
@cole-nyc 5 жыл бұрын
It's Visual Assist's default theme, and it is awesome!
@mithunk2491
@mithunk2491 5 жыл бұрын
Informative video. Can you please make a video on Exception handling and how to use them in programs?
@b0570nk4
@b0570nk4 5 жыл бұрын
So what happens when you disable RTTI and use static_cast() on a mismatching instance?
@BOTHLine
@BOTHLine 4 жыл бұрын
How do virtual functions work if RTTI are turned off? how does Entity* e = Player(); know that it has to perform the overridden function in Player?
@BOTHLine
@BOTHLine 4 жыл бұрын
@Peterolen so the object Entity* e = Player(); contains a pointer to a Player-specific table with all the overwritten functions? Wouldn't be there some information about the class itself, so you can find out it still is a Player object? It's hard for me to grasp why virtual function overloading works but dynamic cast wouldn't
@nickvirus9463
@nickvirus9463 5 жыл бұрын
keep going!
@darkferiousity
@darkferiousity 2 жыл бұрын
Interesting I am just wondering you had to add in a virtual function to let it know it is a polymorphic type, but is there a way of just stating after class that it is polymorphic? Thank you Cherno love watching your videos dude.
@cmdlp4178
@cmdlp4178 5 жыл бұрын
Every virtual base class should contain at least a virtual destructor! Like 》~Entity()《. You need this function, to enable destruction of the subclass when destroyed from the baseclass.
@SomeDeath1990
@SomeDeath1990 5 жыл бұрын
thanks papaiwannou
@Lastrevio
@Lastrevio 5 жыл бұрын
2:29
@semicolon4671
@semicolon4671 5 жыл бұрын
I can smell an entity component system 😊
@climatechangedoesntbargain9140
@climatechangedoesntbargain9140 5 жыл бұрын
what?
@pooria_garrett3020
@pooria_garrett3020 5 жыл бұрын
@@climatechangedoesntbargain9140 ECS a system used in game engines
@paulcook748
@paulcook748 5 жыл бұрын
I quite like your style of teaching. All very good. I don't have time to watch the entire thing right now but I do hope at some point you strongly make the point that in 99% of cases, if you are finding yourself using dynamic_cast then it is because your design is flawed. I admit I cheat with this for work when I'm in a hurry but dynamic_cast has almost no legitimate uses in a well designed codebase. Google Liskov's Substitution Principle if you wish a more thorough understanding of why.
@1Infinitive1
@1Infinitive1 5 жыл бұрын
yeah, if ur using dynamic cast reconsider ur design choices
@yodaddy05
@yodaddy05 4 жыл бұрын
how would this work with smart pointers, thank you!
@yodaddy05
@yodaddy05 4 жыл бұрын
@Peterolen thank u so much!
@bestganesh
@bestganesh 3 жыл бұрын
I really got a good insight after watching this video twice...
@Red-di7zb
@Red-di7zb 5 жыл бұрын
Almost first, but no and this is at 2:35 am just to see a such amazing video.
@hypermeero4782
@hypermeero4782 3 жыл бұрын
why are you amazing?
@AntiWanted
@AntiWanted 3 жыл бұрын
Nice
@kajonpongpoopamonkaipob6153
@kajonpongpoopamonkaipob6153 5 жыл бұрын
Teach for design patturns please
@mrhidetf2
@mrhidetf2 5 жыл бұрын
First second is the begging of ricardoflick
@timelesstrance3720
@timelesstrance3720 5 жыл бұрын
More on principle of design
@doomstothfilms
@doomstothfilms 2 жыл бұрын
I really hate that you can explain more in 10 minutes than a professor can in 2 hours.
@kacperozieblowski3809
@kacperozieblowski3809 5 жыл бұрын
musics good mhheen
@orocimarosay1447
@orocimarosay1447 5 жыл бұрын
well I guess c style cast in this context is undefined behaviour so it won't probably work in a context where entity and player look the same
@nickvirus9463
@nickvirus9463 5 жыл бұрын
gsl next?
@shivaprasad761
@shivaprasad761 5 жыл бұрын
Yay c++
@MsJavaWolf
@MsJavaWolf 5 жыл бұрын
Really happy that you are doing C++ videos again, I really like your videos but I am simply not very interested in game engines. One thing that could have been added to the video though would have been casting of references.
@nielsvs2203
@nielsvs2203 5 жыл бұрын
MsJavaWolf personally I'm not a fan of casting references. While yes it's possible, it easily breaks. Take dynamic_cast for example, when this fails on a pointer, it will return null which you can easily check for and handle, but when it fails on a reference it will throw a std::bad_cast which is slightly more hassle to handle. Let me know your thoughts
@MsJavaWolf
@MsJavaWolf 5 жыл бұрын
@@nielsvs2203 I don't really use dynamic_cast that often in general, but when I do yes, I use pointers more often.
@rcookie5128
@rcookie5128 5 жыл бұрын
*Appreciation comment*
@ultimatesoup
@ultimatesoup Жыл бұрын
Be warned people watching this, if you find yourself using dynamic cast a lot, there is probably something wrong with your design. It has its uses, but legitimate situations for it are few and far between. There is also a slight performance hit for it because the check is done during runtime
@MrNsaysHi
@MrNsaysHi 5 жыл бұрын
actually is dolan.
@lolnyanterts
@lolnyanterts 5 жыл бұрын
c++20!!!!!!!!!!!
BENCHMARKING in C++ (how to measure performance)
14:52
The Cherno
Рет қаралды 160 М.
Casting in C++
13:25
The Cherno
Рет қаралды 190 М.
UFC 310 : Рахмонов VS Мачадо Гэрри
05:00
Setanta Sports UFC
Рет қаралды 1,2 МЛН
I did a C++ University Assignment
50:23
The Cherno
Рет қаралды 302 М.
Dynamic_cast In C++ | How To Use dynamic_cast In C++?
19:07
Ranjan-Beginner's Guide
Рет қаралды 1,5 М.
SINGLETONS in C++
19:16
The Cherno
Рет қаралды 206 М.
Copying and Copy Constructors in C++
20:52
The Cherno
Рет қаралды 441 М.
Cursed C++ Casts
17:41
Logan Smith
Рет қаралды 75 М.
are "smart pointers" actually smart?
9:44
Low Level
Рет қаралды 81 М.
Writing My Own Malloc in C
2:07:13
Tsoding Daily
Рет қаралды 217 М.
std::move and the Move Assignment Operator in C++
16:06
The Cherno
Рет қаралды 179 М.