- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
When should static_cast, dynamic_cast, const_cast and reinterpret_cast be used?
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.
static_cast
This is used for the normal/ordinary type conversion. This is also the cast responsible for implicit type coercion 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.
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
- When should static_cast, dynamic_cast, const_cast and reinterpret_cast be used in C++?
- Dynamic_cast and static_cast in C++
- Regular cast vs. static_cast vs. dynamic_cast in C++
- Regular cast vs. static_cast vs. dynamic_cast in C++ program
- When to use new operator in C++ and when it should not be used?
- When should Capital Cash Flow (CCF) approach be used in evaluating a project?
- When should a sequential model be used with Tensorflow in Python? Give an example
- How should strace be used on Linux?
- When should a Call Option be exercised?
- When should a Put Option be exercised?
- Among id, name, xpath and css, which locator should be used?
- Why should fossil fuels like coal and petroleum be used judiciously?
- Why should fossil fuels be used with care?
- Which equals operator (== vs ===) should be used in JavaScript?
- Which MySQL Datatype should be used for storing BloodType?
