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

The Archive Base Latest Questions

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

I understand that derived class is type compatible with a pointer to its base

  • 0

I understand that derived class is type compatible with a pointer to its base class. In the given sample code, new bar object construction takes place calling foo::foo() followed by bar::bar(). In the respective constructors, I am allocating resources to class members foo::int *a and bar::int *b.

Now I am initializing thus constructed object to base class type. With obj, I can call the base class destructor but not the derived class destructor. So, how can I deallocate the derived class resources in this case? Is this not a memory leak ?

#include <iostream>
class foo
{
    int *a;
public:
    foo()
    {
        a = new int[5];
        std::cout << "\n foo constructor" << std::endl;
    }
    ~foo()
    {
        std::cout << "\n foo destructor" << std::endl;
        delete[] a;
    }
};

class bar : public foo
{
    int *b;
public:
    bar()
    {
        b = new int[5];
        std::cout << "\n bar constructor" << std::endl;
    }
    ~bar()
    {
        std::cout << "\n bar destructor" << std::endl;
        delete[] b;
    }
};

int main()
{
    foo *obj = new bar; // Derived class object is type compatible with base class

    delete obj; // Equivalent to obj->~foo();
    return 0;
}

Thanks.

  • 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:03:20+00:00Added an answer on May 19, 2026 at 1:03 am

    This is where the idea of the “virtual destructor” comes in. Technically speaking, if you delete an object through a pointer of a base class type, you must mark that base class destructor virtual or the result is undefined. If you do mark the destructor virtual, the meaning is different from other virtual functions. Instead of meaning “derived classes override this behavior,” it means “when deleting this object through a base class pointer, call the derived destructors before calling the base constructor.” This is the behavior you want.

    As a general rule, any class you define that you plan on subclassing should have a virtual destructor to prevent this sort of problem.

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

Sidebar

Related Questions

I understand that when catch is followed by object argument derived from base exception
I understand that the List<> of derived class cannot be directly assigned to List<>
I understand that the member names of a base-class template are hidden within the
I understand that static_cast can convert between base and derived and between derived and
Trying to create a Driver type class where, below, Base is the Driver that
I have the following code that doesn't compile. class Base { public: virtual ~Base()
I understand that we can set the various style attributes from code behind using
I am attempting to store objects derived from a templated base class in an
Say we have a base class and a derived . So: class base {
Let's say you have some Java code as follows: public class Base{ public void

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.