Jump to content

  • Log In with Google      Sign In   
  • Create Account

#ActualMatt-D

Posted 23 June 2013 - 12:16 PM

Note:
 

This will only work if you dereference the pointers to the objects. Only then will typeid work with the dynamic type of objects, instead of just giving you Circle* and Shape* back. – Seth Carnegie

 

 

http://stackoverflow.com/questions/12588264/static-cast-and-rtti-vs-dynamic-cast

 

Try this instead: http://ideone.com/dDRNFY

 

See how this works in the example here: http://en.cppreference.com/w/cpp/types/type_info/operator_cmp

 

Also used C++11's nullptr pointer literal:

http://en.cppreference.com/w/cpp/language/nullptr

#include <iostream>
#include <typeinfo>

class Shape { public: virtual ~Shape() {}; };
class Circle : public Shape {};
class Square : public Shape {};
class Other {};

int main() {
  Circle c;
  Shape* s = &c; // Upcast: normal and OK
  // More explicit but unnecessary:
  s = static_cast<Shape*>(&c);
  // (Since upcasting is such a safe and common
  // operation, the cast becomes cluttering)
  Circle* cp = nullptr;
  Square* sp = nullptr;
  // Static Navigation of class hierarchies
  // requires extra type information:
  // C++ RTTI
  if (typeid(*s) == typeid(Circle))
    cp = static_cast<Circle*>(s);
  if (typeid(*s) == typeid(Square))
    sp = static_cast<Square*>(s);
  std::cout << "cp = " << cp << std::endl;
  std::cout << "sp = " << sp << std::endl;
  if (cp) std::cout << "It's a circle!" << std::endl;
  if (sp) std::cout << "It's a square!" << std::endl;
  // Static navigation is ONLY an efficiency hack;
  // dynamic_cast is always safer. However:
  // Other* op = static_cast<Other*>(s);
  // Conveniently gives an error message, while
  // that would be undefined behavior (UB)!
  // Other* op2 = (Other*)s;
  // does not
  // AND *nothing* is guaranteed if we uncomment the UB
  // INCLUDING *anything* about the lines *before* the UB (yes, really!)
}

#2Matt-D

Posted 23 June 2013 - 12:15 PM

Note:
 

This will only work if you dereference the pointers to the objects. Only then will typeid work with the dynamic type of objects, instead of just giving you Circle* and Shape* back. – Seth Carnegie

 

 

http://stackoverflow.com/questions/12588264/static-cast-and-rtti-vs-dynamic-cast

 

Try this instead: http://ideone.com/dDRNFY

 

See how this works in the example here: http://en.cppreference.com/w/cpp/types/type_info

 

Also used C++11's nullptr pointer literal:

http://en.cppreference.com/w/cpp/language/nullptr

#include <iostream>
#include <typeinfo>

class Shape { public: virtual ~Shape() {}; };
class Circle : public Shape {};
class Square : public Shape {};
class Other {};

int main() {
  Circle c;
  Shape* s = &c; // Upcast: normal and OK
  // More explicit but unnecessary:
  s = static_cast<Shape*>(&c);
  // (Since upcasting is such a safe and common
  // operation, the cast becomes cluttering)
  Circle* cp = nullptr;
  Square* sp = nullptr;
  // Static Navigation of class hierarchies
  // requires extra type information:
  // C++ RTTI
  if (typeid(*s) == typeid(Circle))
    cp = static_cast<Circle*>(s);
  if (typeid(*s) == typeid(Square))
    sp = static_cast<Square*>(s);
  std::cout << "cp = " << cp << std::endl;
  std::cout << "sp = " << sp << std::endl;
  if (cp) std::cout << "It's a circle!" << std::endl;
  if (sp) std::cout << "It's a square!" << std::endl;
  // Static navigation is ONLY an efficiency hack;
  // dynamic_cast is always safer. However:
  // Other* op = static_cast<Other*>(s);
  // Conveniently gives an error message, while
  // that would be undefined behavior (UB)!
  // Other* op2 = (Other*)s;
  // does not
  // AND *nothing* is guaranteed if we uncomment the UB
  // INCLUDING *anything* about the lines *before* the UB (yes, really!)
}

#1Matt-D

Posted 23 June 2013 - 12:14 PM

Note:
 

This will only work if you dereference the pointers to the objects. Only then will typeid work with the dynamic type of objects, instead of just giving you Circle* and Shape* back. – Seth Carnegie

 

 

http://stackoverflow.com/questions/12588264/static-cast-and-rtti-vs-dynamic-cast

 

Try this instead: http://ideone.com/dDRNFY

 

See how this works in the example here: http://en.cppreference.com/w/cpp/types/type_info

 

Also used C++11's nullptr pointer literal:

http://en.cppreference.com/w/cpp/language/nullptr

#include <iostream>
#include <typeinfo>

class Shape { public: virtual ~Shape() {}; };
class Circle : public Shape {};
class Square : public Shape {};
class Other {};

int main() {
  Circle c;
  Shape* s = &c; // Upcast: normal and OK
  // More explicit but unnecessary:
  s = static_cast<Shape*>(&c);
  // (Since upcasting is such a safe and common
  // operation, the cast becomes cluttering)
  Circle* cp = nullptr;
  Square* sp = nullptr;
  // Static Navigation of class hierarchies
  // requires extra type information:
  // C++ RTTI
  if (typeid(*s) == typeid(Circle))
    cp = static_cast<Circle*>(s);
  if (typeid(*s) == typeid(Square))
    sp = static_cast<Square*>(s);
  std::cout << "cp = " << cp << std::endl;
  std::cout << "sp = " << sp << std::endl;
  if (cp) std::cout << "It's a circle!" << std::endl;
  if (sp) std::cout << "It's a square!" << std::endl;
  // Static navigation is ONLY an efficiency hack;
  // dynamic_cast is always safer. However:
  // Other* op = static_cast<Other*>(s);
  // Conveniently gives an error message, while
  //Other* op2 = (Other*)s;
  // does not
}

PARTNERS