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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T19:59:07+00:00 2026-05-13T19:59:07+00:00

Am I right in assuming that C-style casts (which are discouraged) are nothing but

  • 0

Am I right in assuming that C-style casts (which are discouraged) are nothing but reinterpret_casts? Using the latter is visually striking and easy to search when looking for nasty casts, and hence it’s recommended over C-style casts?

If casting away const using const_cast and writing to a originally const object is undefined, what is the purpose of const_cast?

Note: I know that Bjarne rightly condemns casting operations that they are unsafe and even goes to the extent of stating “An ugly operation should have an ugly syntactic form.” and hence the verbosity of casting operators in C++. So I’ll try to minimize their usage. Promise. 🙂

  • 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-05-13T19:59:07+00:00Added an answer on May 13, 2026 at 7:59 pm

    No. A C cast can do the equivalent of a const_cast, a static_cast, a reinterpret_cast, or a combination thereof. In case that wasn’t quite enough, it can also do at least one minor trick that no combination of the newer casts can do at all!

    You can use const_cast with defined results if the original variable is defined without const, but all you have is a const pointer or reference to that object. OTOH, if you think you have a good reason to use a const_cast, chances are that you should really look up mutable instead.

    Edit: I suppose I should have said it right off, but a C-style cast can convert to an an inaccessible base class. For example, consider something like:

    [Edit: I’m updating the code to something that’ll compile and (usually) demonstrate problem. ]

    #include <iostream>
    
    class base1 {
    public:
        virtual void print() { std::cout << "base 1\n"; }
    };
    
    class base2 {
    public:
       virtual void print() { std::cout << "base 2\n"; }
    };
    
    class derived : base1, base2 {}; // note: private inheritance
    
    int main() {    
        derived *d = new derived;
        base1 *b1 = (base1 *)d;    // allowed
        b1->print();    // prints "base 1"
        base2 *b2 = (base2 *)d;    // also allowed
        b2->print();    // prints "base 2"
    
    //    base1 *bb1 = static_cast<base *>(d);  // not allowed: base is inaccessible
    
        // Using `reinterpret_cast` allows the code to compile.
        // Unfortunately the result is different, and normally won't work. 
        base1 *bb2 = reinterpret_cast<base1 *>(d);
        bb2->print();   // may cause nasal demons.
    
        base2 *bb3 = reinterpret_cast<base2 *>(d); 
        bb3->print();   // likewise
        return 0;
    }
    

    The code using the reinterpret_casts will compile — but attempting to use the result (of at lest one of the two) will cause a major problem. The reinterpret_cast takes the base address of the derived object and attempts to treat it as if it was the specified type of base object — and since (at most) one base object can actually exist at that address, trying to treat it as the other can/will cause major problems. Edit: In this case, the classes are essentially identical except for what they print, so although anything could happen, with most compilers, both of the last two will print out “base 1”. The reinterpret_cast takes whatever happens to be at that address and tries to use it as the specified type. In this case, I’ve (tried to) make that do something harmless but visible. In real code, the result probably won’t be so pretty.

    The C-style cast will work like a static_cast would if the code had used public inheritance instead of private — i.e. it’s aware of where in the derived class each base class object “lives”, and adjusts the result, so each resulting pointer will work because it’s been adjusted to point at the right place.

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

Sidebar

Related Questions

Am I right in assuming that adding/removing elements to an std::map does not effect
I have a feeling that this probably is not possible using strictly CSS, but,
Am I right assuming that session variables are available on the same page even
Am I right in assuming that class D { /* ... */ }; int
Assuming the binding is right and the image files are where they shuld be,
Right now, I've just some code which fetches the picture from the URL directly.
Right now I have a script that will get the last five files in
Right now I have a function, in a class that is used to listen
I have defined a GWT module that includes an external javascript file using tag.
Trying to figure out how to do this. I have the style but I'd

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.