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

  • Home
  • SEARCH
  • 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 8051129
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T07:14:14+00:00 2026-06-05T07:14:14+00:00

Assuming we have : class A { protected: int* iArr; float *fArr; public: A(int*

  • 0

Assuming we have :

class A {
protected:
    int* iArr;
    float *fArr;
public:
    A(int* iArr,float *fArr);
    ~A();
}

class B : public A {
private:
    double *dArr;
public:
    B(int* iArr,float *fArr,double *dArr);
    ~B();
}

my intuation was that it would only invoke B’s destructor but when i ran it on Visual C++ i saw that when destructing an instance of B it calls both A and then B destructor.

What is the correct way to write destructor in a derived class then? do i always need to assume parent class would take care of deleting everything but what only the derived class have?

Edit:

  1. if it is so, then what if the child extending parent class only with overriding functions,does that mean that i leave the child’s destructor empty?

  2. what if i want to change that? i mean what if i want only child destructor to be called? is there a way to do that?

  • 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-05T07:14:15+00:00Added an answer on June 5, 2026 at 7:14 am

    The destructor of A should be virtual. Your problem lies that if you do the following:

    A* b = new B( iArr, fArr, dArr );
    delete b;
    

    Then B’s destructor won’t get called because b while being instantiated as a B looks like an A and B’s destructor doesn’t have an entry in the virtual table which means it doesn’t know its there (ie it thinks its just an A)

    Edit:

    In answer to 1 you could leave the destructor empty or you could not define it.

    In answer to 2, you can’t really do that. You’d be best off having a virtual protected function called “Destroy” or something thats called from the base class destructor. That is the only way I can think of to control destruction like you describe.

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

Sidebar

Related Questions

Assuming that I have a class named Class , And I would like to
Assuming I have a instance of class A (name it AI) that has an
Assuming I have a struct: struct Vector { public int X, Y; // ...
Assuming I have a class with a method that takes a System.Linq.Expressions.Expression as a
I have two simplest of Entities, similar to: public class User { int id;
Assuming I have a class A as follows: class A{ int id; int getId(){};
Assuming I have a class like this: class Database { public static $hostname =
Assuming I have: public abstract class A { public abstract string Name { get;
Assuming I have the following: [Serializable] public class Foo { public Bar bar {
Assuming I have a class like public class FooImpl { public void bar(){}; }

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.