c++ - Regular cast vs. static_cast vs. dynamic_cast

c++ - Regular cast vs. static_cast vs. dynamic_cast

This question already has an answer here: When should static_cast, dynamic_cast, const_cast and reinterpret_cast be used? 8 answers I've been writing C and C++ code for almost twenty years, but there'…


c++ - Why use static_cast<int>(x) instead of(int)x?

I've heard that the static_cast function should be preferred to C-style or simple function-style casting. Is this true? Why?…


C++ casting programmatically:can it be done?

Let's say I have a Base class and several Derived classes. Is there any way to cast an object to one of the derived classes without the need to write something like this:string typename=typeid(*…


c++ - Should I use static_cast or reinterpret_cast when casting a void* to whatever

Both static_cast and reinterpret_cast seem to work fine for casting void* to another pointer type. Is there a good reason to favor one over the other?…


c++ - When should static_cast, dynamic_cast, const_cast and reinterpret_cast be used?

What are the proper uses of: static_cast dynamic_cast const_cast reinterpret_cast C-style cast(type)value Function-style cast type(value) How does one decide which to use in which specific cases?…



c++ - Is const_cast safe?

I can't find much information on const_cast. The only info I could find(on Stack Overflow) is: The const_cast<>() is used to add/remove const(ness)(or volatile-ness) of a variable. This makes me…


Function pointer to class member function problems

First of all I have to admit that my programming skills are pretty limited and I took over a(really small) existing C++ OOP project where I try to push my own stuff in. Unfortunately I'm experiencin…


static_cast<int>(foo) vs.(int)foo

Could somebody please elaborate on the differences?…


Is it OK to use C-style cast for built-in types?

After reading here a lot of answers about C-style casting in C++ I still have one little question. Can I use C-style casting for built-in types like long x=(long)y; or it's still considered bad and d…


c++ - When to use reinterpret_cast?

I am little confused with the applicability of reinterpret_cast vs static_cast. From what I have read the general rules are to use static cast when the types can be interpreted at compile time hence t…




cast reinterpret_cast int geeksforgeeks type conversion casting class example dynamic_cast