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

  • SEARCH
  • Home
  • 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 7713189
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T01:48:41+00:00 2026-06-01T01:48:41+00:00

I know about the existence of static_cast , dynamic_cast . But I can’t seem

  • 0

I know about the existence of static_cast, dynamic_cast. But I can’t seem to find out a concrete reason to convince myself about why cast from base to derive or vice versa?

Any example in code would be appreciated.

UPDATE

class Base
{
    public:
        void foo();
    private:
        int _x;
};

class Derive: Base
{

};

Base *b = new Derive;  //will b behave the same as if it's a Derive *?
Derive *d = new Base;  //I think d can't behave like a Derive * but Base *, right?
  • 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-01T01:48:42+00:00Added an answer on June 1, 2026 at 1:48 am

    Actually, those casts are obvious marks of something unsual going on in the code, so in a perfect world, you shouldn’t use them.

    But in some cases they are the right tool for the job.

    For static_cast, there are basically 2 cases:

    1. Primitive conversion.

    When you really need some integer number to be processed in a calculus involving floats.

    float ratio = static_cast<float>( pixel_pos.x ) / static_cast<float>( pixel_pos.y ); // x and y are integers because pixel positions are absolute, but we need to get a floating point value here
    

    2. You got an object from some external API and you want to get the specific child-type.

    Thing* thing = factory.create( "shoe" ); // Even if I don't have it's real type, I know it's a shoe!
    
    Shoe* shoe = static_cast<Shoe*>( thing ); // I need to use Shoe interface so lets cast it.
    

    If you designed the system, maybe you could have done it better to avoid the cast. But if you didn’t and the API you’re using provide the base type as a way for you to work with it, then you don’t have any other choice than to cast.

    static_cast is useful also because it lets you assume something at compile time, so you should use it first because it requires you to be sure about what you are doing.

    3.You don’t know what is the real type of the object.

    However, there is a specific case when you need to know the real type at runtime because there is no way for you to know it at another time. Typical case is when you’re receiving some kind of objects from an external system and there is no other information about the real type of the object

    void on_something_happen( const Event& event ) // callback triggered when an event occured in the library system this callback is plugged in
    {
         // here I want to manage two cases
         ThingEvent* thing_event = dynamic_cast<ThingEvent*>( &event );
    
        if( thing_event )
        {
            // do my thing
        }
        else
        {
    
            // ok this event HAVE TO be a FooEvent, otherwise this should crash
            FooEvent& foo_event = dynamic_cast<FooEvent&>( event );
            // do my thing
    
        }
    
    
    
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know about http://www.upcdatabase.com/ , but it's quite incomplete. Where popular mobile apps getting
I know about primitives and objects living on the heap, but how does the
I know about the ADODB.Stream object . But what I really want is a
I know about Options->Fonts and Colors. But there is no option for F# strings(i
Do you possibly know about the existence of a free and open sourced Delphi
I know about Application.Exit() but what if I'm not in a windows forms application
I know about git diff branch1 branch2 . However, I would like to see
I know about the LINEABOVE and LINEBELOW styles, i was wondering if there is
I know about map/reduce alghoritm and its use. It's using functions that are called
I know about Allow Triggers to Fire Others server setting that allows the action

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.