Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8582023
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T21:11:24+00:00 2026-06-11T21:11:24+00:00

Please observe the below code. As far as i know, dynamic_cast is slower than

  • 0

Please observe the below code. As far as i know, dynamic_cast is slower than static_cast. Because it evaluates the type at runtime.
My doubt here is if we use static_cast with typeid() as below , will it takes same time as dynamic cast ??
Will it be faster than dynamic_cast ?

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

Static cast with RTTI:

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 = 0;
Square* sp = 0;

// Static Navigation of class hierarchies
// requires extra type information:
if(typeid(s) == typeid(cp)) // C++ RTTI
    cp = static_cast<Circle*>(s);
if(typeid(s) == typeid(sp))
    sp = static_cast<Square*>(s);
if(cp != 0)
    cout << "It's a circle!" << endl;
if(sp != 0)
    cout << "It's a square!" << endl;

Dynamic cast:

Circle c;
Shape* s = &c; // Upcast: normal and OK

s = &c;
Circle* cp = 0;
Square* sp = 0;
cp = dynamic_cast<Circle*>(s);
    if(cp != 0)
    cout << "It's a circle!" << endl;
sp = dynamic_cast<Square*>(s);
if(sp != 0)
    cout << "It's a square!" << endl;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T21:11:25+00:00Added an answer on June 11, 2026 at 9:11 pm

    It is faster to test the type and then do the static_cast, but the operations are not equivalent as that will only allow downcast to the most derived type (any intermediate level will not be matched with the typeid). I would use dynamic_cast as it is more robust (will not break if someone extends your type and passes a pointer, for example).

    If performance of dynamic_cast is an issue in your application, you should reconsider the design. While typeid + static_cast is faster than dynamic_cast, not having to switch on the runtime type of the object is faster than any of them.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Please i need help, this code below works fine on my localhost, php5.3+ but
Why does this happen? Please observe the following code: static class StringExtension { public
Please observe the following markup: <Style TargetType={x:Type MenuItem} x:Key=...> <Setter Property=HeaderTemplate> <Setter.Value> <DataTemplate> <ContentPresenter
please have a look at the following code import java.util.ArrayList; import java.util.List; public class
Please let me know what are default locations for Downloads folder on devices with
I have below code. <table> <tr> <td class=bgimg valign=middle width=10% title=some title nowrap> <img
I have this code. <html> <head> <script src=//ajax.googleapis.com/ajax/libs/prototype/1.7.1.0/prototype.js> </script> <script> Event.observe('clickme', 'click', function(event) {
Please observe this fiddle: http://jsfiddle.net/8nrPm/3/ I have set the width of the Animation button
Please note that Object.Watch and Object.Observe are both deprecated now (as of Jun 2018).
Please observe the following behavior: a = ufoo b = ub\xe1r # \xe1 is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.