473,387 Members | 1,295 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Destructors of Derived classes

I seem to be having a problem with the way destructors are called with
derived classes. Below is a short example of what I'm trying to do.
In the example a is the base class. It has a function 'action' which
destroys the object. As I understand derived classes and destructors,
the destructor of the derived class should be called first, then the
destructor of the base class. Really, for my purposes, order doesn't
matter. In my example here I would only get the output "a:~a". It
never executes the destructor of the derived class.

The code below is just an example to simply show an idea, it hasn't
been compiled.
class a
{
public:
action();
~a();

};

class a_derivative:a
{
public:
~a_derivative();

};
a:~a()
{
printf("a:~a");
}

a:action()
{
delete this;
}
a_derivative:~a_derivative()
{
printf("a_derivative:~a_derivative");
}
main()
{
a_derivative* my_a;

my_a = new a_derivative();

my_a->action();

}
Jul 19 '05 #1
1 2013
On 30 Jul 2003 08:44:36 -0700, ha******@yahoo.com (Mike) wrote:
I seem to be having a problem with the way destructors are called with
derived classes. Below is a short example of what I'm trying to do.
In the example a is the base class. It has a function 'action' which
destroys the object. As I understand derived classes and destructors,
the destructor of the derived class should be called first, then the
destructor of the base class. Really, for my purposes, order doesn't
matter. In my example here I would only get the output "a:~a". It
never executes the destructor of the derived class.

The code below is just an example to simply show an idea, it hasn't
been compiled.
class a
{
public:
action();
void action();
~a();
virtual ~a();


};

class a_derivative:a
{
public:
~a_derivative();

};
a:~a()
{
printf("a:~a");
}

a:action()
{
delete this;
}
a_derivative:~a_derivative()
{
printf("a_derivative:~a_derivative");
}
main()
int main()
{
a_derivative* my_a;

my_a = new a_derivative();

my_a->action();

}


Jul 19 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Nuno Barros | last post by:
Cn someone tell me if when i call the destructor of a derivated class, the destructor of the base class is called implicitly? Or shall i call the destructor by myself? Thanks in advance ...
7
by: qazmlp | last post by:
When a member function is declared as virtual in the base class, the derived class versions of it are always treated as virtual. I am just wondering, why the same concept was not used for the...
15
by: christopher diggins | last post by:
I posted to my blog a special pointer class work-around for inheriting from base classes without virtual destructors. I was wondering if there is any other similar work, and whether there are any...
23
by: heted7 | last post by:
Hi, Most of the books on C++ say something like this: "A virtual destructor should be defined if the class contains at least one virtual member function." My question is: why is it only for...
5
by: Alok | last post by:
hii Would somebody clear my doubts on following qustions . What are virtual constructors and virtual destructors ? Why can't we have virtual constructors ? What are demerits of inheritence in...
11
by: jquesnelle | last post by:
why don't the std classes have virtual destructors? doesn't it make sense to want to inherit from these classes and add/change functionality?
2
by: swarup | last post by:
Hi, Can any body pls tell me in what order the destructors will execute. I'm having two classes and the object is of the derived class.
12
by: Avalon1178 | last post by:
Hello, Are default destructors virtual? In other words, say I have "class A" and "class B : public A", and I have the code below: A * a = new A; B * b = new B; a = b;
6
by: Jeff Newman | last post by:
Hello, Could anyone explain to me why the following class's destructor shows up as having multiple branches? (At least as judged by gcov 4.1.2 when compiled with gcc 4.1.2 ): struct blah {...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.