
- 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
Dynamic_cast and static_cast in C++
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. This can cast related type classes. If the types are not same it will generate some error.
Example
#include<iostream> using namespace std; class Base {}; class Derived : public Base {}; class MyClass {}; main(){ Derived* d = new Derived; Base* b = static_cast<Base*>(d); // this line will work properly MyClass* x = static_cast<MyClass*>(d); // ERROR will be generated during compilation }
Output
[Error] invalid static_cast from type 'Derived*' to type 'MyClass*'
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.
Example
#include<iostream> using namespace std; class MyClass1 { public: virtual void print()const { cout << "This is from MyClass1\n"; } }; class MyClass2 { public: virtual void print()const { cout << "This is from MyClass2\n"; } }; class MyClass3: public MyClass1, public MyClass2 { public: void print()const { cout << "This is from MyClass3\n"; } }; int main(){ MyClass1* a = new MyClass1; MyClass2* b = new MyClass2; MyClass3* c = new MyClass3; a -> print(); b -> print(); c -> print(); b = dynamic_cast< MyClass2*>(a); //This cast will be failed if (b) b->print(); else cout << "no MyClass2\n"; a = c; a -> print(); //Printing from MyClass3 b = dynamic_cast< MyClass2*>(a); //Successfully casting is done if (b) b -> print(); else cout << "no MyClass2\n"; }
Output
This is from MyClass1 This is from MyClass2 This is from MyClass3 no MyClass2 This is from MyClass3 This is from MyClass3
- Related Articles
- Const cast in C++
- CAST function in Cassandra
- C# Cast method
- MySQL CAST as DATE?
- What is Cast Operator () in C#?
- MySQL - CAST DECIMAL to INT?
- How to use cast() in Android sqlite?
- Can we cast reference variables in Java?
- How Can MySQL CAST handle overflow?
- Regular cast vs. static_cast vs. dynamic_cast in C++
- What is a type cast in C/C++?
- Cast to ObjectId failed for value in MongoDB?
- PHP How to cast variable to array?
- Static binding and dynamic binding in Java
- How to cast DATETIME as a DATE in MySQL?
- How to cast from VARCHAR to INT in MySQL?

Advertisements