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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T01:44:30+00:00 2026-05-24T01:44:30+00:00

I understand that static_cast can convert between base and derived and between derived and

  • 0

I understand that static_cast can convert between base and derived and between derived and base. dynamic_cast will check resulting object is a ‘complete’ object.

dynamic_cast uses RTTI feature. But how does static_cast work? What does ‘compatible types’ mean?

My question was really about what compatible types meant. But I am happy that have learnt something from posts. This sample demonstrates how the compiler interprets compatible types. Last few lines are most interesting. note interesting results.

#include <iostream>
#include <exception>
using namespace std;

class CBase { virtual void dummy() {} };
class CDerived: public CBase {  public: CDerived() : a(20) {} int a; };
class CDerived2: public CBase { public: CDerived2() : b(7) {} int b; };
class CDerived3: public CBase { public: CDerived3() : c('A') {} char c; };
class CNotDerived { int doit() const { return 9; } };

int main () {
  try {
      CBase * pba = new CDerived;
      CBase * pbb = new CBase;
      CDerived * pd;
      CNotDerived* pnot = new CNotDerived;
      CDerived2* pd2 = 0; 
      CDerived2* pdx = new CDerived2;
      CDerived3* pd3 = 0;


      pd = dynamic_cast<CDerived*>(pba);
      if (pd==0) cout << "Null pointer on first type-cast" << endl;   //ok

      pd = dynamic_cast<CDerived*>(pbb);
      if (pd==0) cout << "Null pointer on second type-cast" << endl;  //null ptr here

      pd = static_cast<CDerived*>(pbb);  //non-null pointer returned (not really what you want)
      if (pd==0) cout << "Null pointer on third type-cast" << endl;

// pd = dynamic_cast(pnot); //error C2683: ‘dynamic_cast’ : ‘CNotDerived’ is not a polymorphic type
// if (pnot==0) cout << “Null pointer on fourth type-cast” << endl;

// pd = static_cast(pnot); //error C2440: ‘static_cast’ : cannot convert from ‘CNotDerived *’ to ‘CDerived *’
// if (pnot==0) cout << “Null pointer on fourth type-cast” << endl;

      //below lines compiled with ms vs2008 - I believe compiler SHOULD have flagged below as an error - but did not.
      pd2 = static_cast<CDerived2*>(pba); //compiles ok but obviously incorrect
      if (pd2==0) cout << "Null pointer on fourth type-cast" << endl;
      cout << pd2->b << endl;  //compiler had decided to give us CDerived->a value! Incorrect.

      pd2 = static_cast<CDerived2*>(pdx); //compiles ok 
      if (pd2==0) cout << "Null pointer on fourth type-cast" << endl;
      cout << pd2->b << endl;  //gives correct value for b (7)

      pd3 = static_cast<CDerived2*>(pdx); //error C2440: '=' : cannot convert from 'CDerived2 *' to 'CDerived3 *'
      if (pd3==0) cout << "Null pointer on fourth type-cast" << endl;
      cout << pd3->c << endl; 

  } catch (exception& e) {
      cout << "Exception: " << e.what();
  }
  return 0;
}
  • 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-24T01:44:31+00:00Added an answer on May 24, 2026 at 1:44 am

    The main cases are they have a parent-child relationship or are both builtin numeric types. It’s also valid if one object can be constructed from the other, integral types can be converted to enumerated types, and void pointers can be cast to pointer-to-object.

    EDIT for the main cases (I omitted some more obscure cases like pointer-to-member casts):

    5.2.9/2:

    An expression e can be explicitly converted to a type T using a
    static_cast of the form static_cast(e) if the declaration “T t(e);”
    is wellformed, for some invented temporary variable t (8.5).

    5.2.9/4:

    Any expression can be explicitly converted to type “cv void.” The
    expression value is discarded.

    5.2.9/5:

    An lvalue of type “cv1 B”, where B is a class type, can be cast to
    type “reference to cv2 D”, where D is a class derived (clause 10) from
    B, if a valid standard conversion from “pointer to D” to “pointer to
    B” exists (4.10), cv2 is the same cvqualification as, or greater
    cvqualification than, cv1, and B is not a virtual base class of D.

    7.2.9/7:

    A value of integral type can be explicitly converted to an enumeration
    type.

    7.2.9/10:

    An rvalue of type “pointer to cv void” can be explicitly converted to
    a pointer to object type.

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

Sidebar

Related Questions

I understand that Microsoft uses this template when versioning their products: Major.Minor.Build.Revision. Major is
I understand that static method inheritance is not supported in C#. I have also
I understand that some countries have laws regarding website accessibility. In general, what are
I understand that there are several ways to blend XNA and WPF within the
I understand that they are both supposed to be small, but what are the
I understand that server-side validation is an absolute must to prevent malicious users (or
I understand that IronPython is an implementation of Python on the .NET platform just
I understand that an id must be unique within an HTML/XHTML page. For a
I understand that these methods are for pickling/unpickling and have no relation to the
I understand that requests are served by different threads, but do they all come

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.