
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Regular cast vs. static_cast vs. dynamic_cast in C++ program
static_cast − This is used for the normal/ordinary type conversion. This is also the cast responsible for implicit type coersion and can also be called explicitly. You should use it in cases like converting float to int, char to int, etc.
dynamic_cast − This cast is used for handling polymorphism. You only need to use it when you're casting to a derived class. This is exclusively to be used in inheritance when you cast from base class to derived class.
Regular Cast − This is the most powerful cast available in C++ as it combines const_cast, static_cast and reinterpret_cast. but it's also unsafe because it does not use dynamic_cast. This is also called as C-style cast.
Other Available casts
const_cast − can be used to remove or add const to a variable. This can be useful if it is necessary to add/remove constness from a variable.
reinterpret_cast − This is the trickiest to use. It is used for reinterpreting bit patterns and is extremely low level. It's used primarily for things like turning a raw data bit stream into actual data or storing data in the low bits of an aligned pointer.
- Related Articles
- Regular cast vs. static_cast vs. dynamic_cast in C++
- PHP string cast vs strval function, which one should I use?
- C# Cast method
- Const cast in C++
- CAST function in Cassandra
- MySQL CAST as DATE?
- Static vs Dynamic Binding in Java
- MySQL - CAST DECIMAL to INT?
- What is Cast Operator () in C#?
- Static binding vs Dynamic binding in C#
- How Can MySQL CAST handle overflow?
- How to use cast() in Android sqlite?
- Can we cast reference variables in Java?
- C# Program to cast a type to its IEnumerable equivalent
- PHP How to cast variable to array?
