Avatar ·

What is the difference between static_cast, dynamic_cast, reinterpret_cast and C-style cast in C++?

Hi everyone! I just started learning C++ and I can't figure out type casts. In books and forums they write about `static_cast`, `dynamic_cast`, `reinterpret_cast` and also the old C-style cast in parentheses. When I write in C, I just wrote `(int)myDouble` and it worked. But in C++ they tell me that's bad practice and I should use C++-style casts. For example, I have a class hierarchy: ```cpp class Base { public: virtual ~Base() {} }; class Derived : public Base { public: void foo() {} }; ``` I want to convert `Base*` to `Derived*`. It seems I can write `(Derived*)basePtr` and it will compile. But what should I use instead? I heard that `dynamic_cast` can return `nullptr` if the cast fails, while `static_cast` doesn't do that. And there's also `reinterpr