
- 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
Type Casting operators in C++
A cast is a special operator that forces one data type to be converted into another. As an operator, a cast is unary and has the same precedence as any other unary operator.
The most general cast supported by most of the C++ compilers is as follows
(type) expression
Where type is the desired data type. There are other casting operators supported by C++, they are listed below -
? const_cast<type> (expr) The const_cast operator is used to explicitly override const and/or volatile in a cast. The target type must be the same as the source type except for the alteration of its const or volatile attributes. This type of casting manipulates the const attribute of the passed object, either to be set or removed.
? dynamic_cast<type> (expr) The dynamic_cast performs a runtime cast that verifies the validity of the cast. If the cast cannot be made, the cast fails and the expression evaluates to null. A dynamic_cast performs casts on polymorphic types and can cast a A* pointer into a B* pointer only if the object being pointed to actually is a B object.
? reinterpret_cast<type> (expr) The reinterpret_cast operator changes a pointer to any other type of pointer. It also allows casting from pointer to an integer type and vice versa.
? static_cast<type> (expr) The static_cast operator performs a nonpolymorphic cast. For example, it can be used to cast a base class pointer into a derived class pointer.
All of the above-mentioned casting operators will be used while working with classes and objects. For now, try the following example to understand a simple cast operators available in C++. Copy and paste the following C++ program in test.cpp file and compile and run this program.
Example Code
#include <iostream> using namespace std; main() { double a = 21.09399; float b = 10.20; int c ; c = (int) a; cout << "Line 1 - Value of (int)a is :" << c << endl ; c = (int) b; cout << "Line 2 - Value of (int)b is :" << c << endl ; return 0; }
Output
Line 1 - Value of (int)a is :21 Line 2 - Value of (int)b is :10
- Related Articles
- const_cast in C++ - Type casting operators
- What is Casting operators in C++?
- Explicit type casting operator in C++
- What is Type casting in C#?
- Type casting in JavaScript.
- What is the difference between type conversion and type casting in C#?
- Java Type Casting Examples
- Explicit Type Casting in Python Language
- Difference Between Type casting and Type Conversion
- PHP Type Operators
- PHP Casting Variable as Object type in foreach Loop
- HTML5 data-* attribute type casting strings and numbers
- Test type operators in Dart Programming
- What are up-casting and down-casting in Java?
- Java Program to Calculate Sum of Two Byte Values Using Type Casting
