Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 1.dynamic_cast <new_type> (expression)
- 2.reinterpret_cast <new_type> (expression)
- 3.static_cast <new_type> (expression)
- 4.const_cast <new_type> (expression)
- #include <iostream>
- #include <boostpolymorphic_cast.hpp>
- class Base{
- public: virtual ~Base() = default;
- };
- class D1 : public Base{
- public: virtual ~D1() = default;
- };
- class D2 : public Base{
- public: virtual ~D2() = default;
- };
- int main(void)
- {
- D1 d1;
- Base* b = &d1;
- Base* b2 = nullptr;
- std::cout << "n---Testing valid cast---n";
- std::cout << "cast from base to d1: " << boost::polymorphic_cast<D1*>(b) << "n";
- try
- {
- std::cout << "n---Testing invalid cast---n";
- std::cout << "base to d2: " << boost::polymorphic_cast<D2*>(b) << "n";
- }
- catch (...)
- {
- std::cout << "cast from based to d2 failedn";
- }
- try
- {
- std::cout << "n---Testing nullptr cast---n";
- std::cout << "null to d1: " << boost::polymorphic_cast<D1*>(b2) << "n";
- }
- catch (...)
- {
- std::cout << "cast from null to d1 failedn";
- }
- }
- ---Testing valid cast---
- cast from base to d1: 00AFFBD4
- ---Testing invalid cast---
- cast from based to d2 failed
- ---Testing nullptr cast---
- cast from null to d1 failed
Add Comment
Please, Sign In to add comment