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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T03:24:50+00:00 2026-06-13T03:24:50+00:00

Here are 2 codes first: #include<iostream> using namespace std; class A{ public: virtual void

  • 0

Here are 2 codes
first:

#include<iostream>
using namespace std;

class A{
  public:
     virtual void  f()
    {
      cout<<"A"<<endl;
    }
};
class B: public A{
  public:
    virtual void  f()
    {
      cout<<"B"<<endl;
    }
};
int main()
{
  A* pa=new A();
  B* pb=new B();
  A* upCastpa= static_cast<A*>(pb);
  B* downCastpb=static_cast<B*>(pa);
  upCastpa->f();
  downCastpb->f();
  return 1;
}

one display

B
A

Therefore I think what really matters is the objected the pointer pointing to.
However, if I remove virtual form A::f(), like this;

#include<iostream>
using namespace std;

class A{
  public:
     void  f()
    {
      cout<<"A"<<endl;
    }
};
class B: public A{
  public:
    virtual void  f()
    {
      cout<<"B"<<endl;
    }
};
int main()
{
  A* pa=new A();
  B* pb=new B();
  A* upCastpa= static_cast<A*>(pb);
  B* downCastpb=static_cast<B*>(pa);
  upCastpa->f();
  downCastpb->f();
  return 1;
}

The code will displayed
A
“stop”
What happened? If the important thing is the objected the pointer pointing to.

it suppose to display
A
B
instead of corrupted.

what happened?

I really appreciate any advice or instruction. Thanks a lot.

  • 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-13T03:24:52+00:00Added an answer on June 13, 2026 at 3:24 am

    Irrespective of the presence or absence of virtual function:

    A* pa=new A(); 
    B* downCastpb=static_cast<B*>(pa); 
    

    causes an Undefined Behavior.

    When you use static_cast to cast an object to a type that it is not, it causes Undefined behavior. Once you have an code that produces undefined behavior it is useless to try and find reasoning for the output observed. The compiler is free to show any behavior, a crash, a weird result or an absoultely fine working code.You are completely at the mercy of the compiler.

    Reference:

    C++11 Standard 5.2.9 [expr.static.cast]

    A prvalue of type “pointer to cv1 B“, where B is a class type, can be converted to a prvalue of type “pointer to cv2 D“, where D is a class derived from B, if a valid standard conversion from “pointer to D” to “pointer to B” exists, cv2 is the same cv-qualification as, or greater cv-qualification than, cv1, and B
    is neither a virtual base class of D nor a base class of a virtual base class of D. The null pointer value is converted to the null pointer value of the destination type. If the prvalue of type “pointer to cv1 B” points
    to a B that is actually a subobject of an object of type D, the resulting pointer points to the enclosing object
    of type D. Otherwise, the result of the cast is undefined.

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

Sidebar

Related Questions

#include <iostream> using namespace std; class X { public: X() { cout<<Cons<<endl; } X(const
Here is simple linked list code: #include <iostream> using namespace std; class link {
Here is my code. class xyz. #include <iostream> #include <vector> #include <map> using namespace
I am trying to understand the following bit of code: #include<iostream> using namespace std;
Here's sample model classes, which being use with Entity Framework Code First: public class
Here is the code: string str; cin>>str; cout<<first input:<<str<<endl; getline(cin, str); cout<<line input:<<str<<endl; The
I've got an error while using find() function. Here is the code: #include <iostream>
Here is some code I wrote (using GCC's __restrict__ extension to C++): #include <iostream>
Let me first put the code snippets here. I am just using the ASP.NET
First off here is the code! <!DOCTYPE html PUBLIC -//W3C//DTD XHTML 1.0 Strict//EN http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd>

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.