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 41065
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T15:07:19+00:00 2026-05-10T15:07:19+00:00

I’ve heard that the static_cast function should be preferred to C-style or simple function-style

  • 0

I’ve heard that the static_cast function should be preferred to C-style or simple function-style casting. Is this true? Why?

  • 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. 2026-05-10T15:07:19+00:00Added an answer on May 10, 2026 at 3:07 pm

    The main reason is that classic C casts make no distinction between what we call static_cast<>(), reinterpret_cast<>(), const_cast<>(), and dynamic_cast<>(). These four things are completely different.

    A static_cast<>() is usually safe. There is a valid conversion in the language, or an appropriate constructor that makes it possible. The only time it’s a bit risky is when you cast down to an inherited class; you must make sure that the object is actually the descendant that you claim it is, by means external to the language (like a flag in the object). A dynamic_cast<>() is safe as long as the result is checked (pointer) or a possible exception is taken into account (reference).

    A reinterpret_cast<>() (or a const_cast<>()) on the other hand is always dangerous. You tell the compiler: ‘trust me: I know this doesn’t look like a foo (this looks as if it isn’t mutable), but it is’.

    The first problem is that it’s almost impossible to tell which one will occur in a C-style cast without looking at large and disperse pieces of code and knowing all the rules.

    Let’s assume these:

    class CDerivedClass : public CMyBase {...}; class CMyOtherStuff {...} ;  CMyBase  *pSomething; // filled somewhere 

    Now, these two are compiled the same way:

    CDerivedClass *pMyObject; pMyObject = static_cast<CDerivedClass*>(pSomething); // Safe; as long as we checked  pMyObject = (CDerivedClass*)(pSomething); // Same as static_cast<>                                      // Safe; as long as we checked                                      // but harder to read 

    However, let’s see this almost identical code:

    CMyOtherStuff *pOther; pOther = static_cast<CMyOtherStuff*>(pSomething); // Compiler error: Can't convert  pOther = (CMyOtherStuff*)(pSomething);            // No compiler error.                                                   // Same as reinterpret_cast<>                                                   // and it's wrong!!! 

    As you can see, there is no easy way to distinguish between the two situations without knowing a lot about all the classes involved.

    The second problem is that the C-style casts are too hard to locate. In complex expressions it can be very hard to see C-style casts. It is virtually impossible to write an automated tool that needs to locate C-style casts (for example a search tool) without a full blown C++ compiler front-end. On the other hand, it’s easy to search for ‘static_cast<‘ or ‘reinterpret_cast<‘.

    pOther = reinterpret_cast<CMyOtherStuff*>(pSomething);       // No compiler error.       // but the presence of a reinterpret_cast<> is        // like a Siren with Red Flashing Lights in your code.       // The mere typing of it should cause you to feel VERY uncomfortable. 

    That means that, not only are C-style casts more dangerous, but it’s a lot harder to find them all to make sure that they are correct.

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

Sidebar

Ask A Question

Stats

  • Questions 104k
  • Answers 104k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Use the Format event on the ListBox: #1). Register the… May 11, 2026 at 8:35 pm
  • Editorial Team
    Editorial Team added an answer I think you may be looking for: $this->variables[$key] = $value;… May 11, 2026 at 8:35 pm
  • Editorial Team
    Editorial Team added an answer You might look at Phusion Passenger and Ruby Enterprise Edition,… May 11, 2026 at 8:35 pm

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am currently running into a problem where an element is coming back from
Seemingly simple, but I cannot find anything relevant on the web. What is the
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.