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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T23:28:57+00:00 2026-06-11T23:28:57+00:00

I have this code : A * a = new A; a->fun(); delete a;

  • 0

I have this code :

A * a = new A;
a->fun();
delete a;

a = new B;
a->fun();
delete a;

What I need to do is to make it print :

A::fun() //being printed by A's fun()
B::fun() //being printed by B's fun()

without using the virtual keyword. Classes can be altered although the main code has to remain unchanged. How can this be done?

(Also, B is derived from A)

  • 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-11T23:28:59+00:00Added an answer on June 11, 2026 at 11:28 pm

    I hope this is for educational purposes.

    If the destructor of a is not virtual this results in undefined behavior. So the destructor must be virtual.

    If the destructor is virtual, you can use dynamic_cast. So, you can change A::foo to:

    void A::fun()
    {
       if ( dynamic_cast<B*>(this) )
           ((B*)this)->fun();
       else
           std::cout << "A::fun()";
    }
    

    Alternative with non-virtual destructor (undefined behavior if you call delete):

    struct A
    {
            bool isA;
            A(bool isA = true) : isA(isA){};
            void fun();
    };
    struct B : A
    {
            B() : A(false){}
            void fun() {cout<<"B::fun()"<<endl;}
    };
    
    void A::fun()
    {
            if (isA)
                    std::cout << "A::fun()"<<endl;
            else
                    ((B*)this)->fun();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code: hubSpinner.setOnItemSelectedListener(new OnItemSelectedListener() { public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int
I have this code: app = new Application(); app.Visible = false; workbook = app.Workbooks.Add(1);
I have this code: ServerSocket serverSideSocket = new ServerSocket(1234); serverSideSocket.accept(); BufferedReader in = new
i have this code: var list = new List<int>(); for(int i=0;i<10;i++) list.Add(i); for(int i=0;i<10;i++)
I have this code: class LFSeq: # lazy infinite sequence with new elements from
I have this code public static Boolean freq[] = new Boolean[Global.iParameter[2]]; freq[Global.iParameter[2]] = false;
I have this code for an event handler: rbM.setOnCheckedChangeListener(new OnCheckedChangeListener() { public void onCheckedChanged(CompoundButton
I have this code require 'mechanize' @agent = Mechanize.new page = @agent.get('http://something.com/?page=1') next_page =
I have this code : void Main() { System.Timers.Timer t = new System.Timers.Timer (1000);
I have this code to create an ATOM feed Dim xmlResult As New StringBuilder

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.