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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T01:28:13+00:00 2026-05-19T01:28:13+00:00

#include <cstdio> using namespace std; class A { public: virtual void func() { printf(A::func());

  • 0
#include <cstdio>
using namespace std;

class A {
public:
    virtual void func() { printf("A::func()"); }
};

class B : public A {
public:
    virtual void func() { printf("B::func()"); }
};

int main() {
  A a = *(A *)new B();
  a.func();
}

The question is simple: why a->func() calls function in class A even though a contains object of class B?

  • 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-19T01:28:13+00:00Added an answer on May 19, 2026 at 1:28 am
    A a = *(A *)new B();
    a.func();
    

    Here’s what happens in this code, step by step:

    • new B(): a new object of type B is allocated on the free store, resulting in its address
    • (A*): the address of the object is cast to A*, so we have a pointer of type A* actually pointing to an object of type B, which is valid. All OK.
    • A a: here the problems start. A new local object of type A is created on the stack and constructed using the copy constructor A::A(const A&), with the first paremeter being the object created before.
    • The pointer to the original object of type B is lost after this statement, resulting in a memory leak, since it was allocated on the free store with new.
    • a.func() – the method is called on the (local) object of class A.

    If you change the code to:

    A& a = *( A*) new B();
    a.func();
    

    then only one object will be constructed, its pointer will be converted to pointer of type A*, then dereferenced and a new reference will be initialized with this address. The call of the virtual function will then be dynamically resolved to B::func().


    But remember, that you’d still need to free the object since it was allocated with new:

    delete &a;
    

    Which, by the way, will only be correct if A has a virtual destructor, which is required that B::~B() (which luckily is empty here, but it doesn’t need to in the general case) will also be called. If A doesn’t have a virtual destructor, then you’d need to free it by:

    delete (B*)&a;
    

    If you would want to use a pointer, then that’s the same as with the reference. Code:

    A* a = new B(); // actually you don't need an explicit cast here.
    a->func();
    delete (B*)a; // or just delete a; if A has a virtual destructor.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider this code #include <iostream> #include <cstdio> using namespace std; class Dummy { public:
I'm getting compile error in this code #include<iostream> #include<cstdio> #include<string> using namespace std; void
#include <iostream> #include <string> #include <algorithm> #include <cstdlib> #include <cstdio> using namespace std; static
This works: #include <iostream> using namespace std; but this fails: #include <stdio> When is
I wrote some code but I am unable to compile it: #include <cstdio> #include
I'm new to C++ and was working on an assignment for a class. We
I am using cygwin libraries to run C and C++ programs on Windows. gcc
I am trying to implement example of visitor pattern, but I have trouble with
I am trying to print a value of an array element as cout <<
could someone explain why i am getting this error when i am compiling the

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.