473,406 Members | 2,467 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,406 software developers and data experts.

Sequence of constructor calls and destructor calls.

Hi,
Can anybody clarify why destruction happens in reverse way?
for example A<-B<-C, when C's object is constructed, ctor calls will A(),B() and
C(), when this object is destroyed, sequence of calls will be ~C(),~B(),and
~C(). I am wondering why cant' we destroy the object like ctor way?..Are there
any good reasons for this?

thanks,
-Honne
--
Jul 19 '05 #1
2 7772

"Honne Gowda A" <ho***@lucent.com> wrote in message
news:3F***************@lucent.com...
Hi,
Can anybody clarify why destruction happens in reverse way?
for example A<-B<-C, when C's object is constructed, ctor calls will A(),B() and C(), when this object is destroyed, sequence of calls will be ~C(),~B(),and
~C(). I am wondering why cant' we destroy the object like ctor way?..Are there
any good reasons for this?


Logically if you agree that before becoming a C object it first becomes an A,
then B and *then* a C object.
(Constructor call sequence)
Then you should also agree that when it has to destruct it should first become B
from C, and then A from B and then nothing!

If I were to make a pile of books I would build it one over another. But when I
were to remove it I would go the other way from top to bottom, unless I want the
whole pile to fall down :-)

HTH,
J.Schafer
Jul 19 '05 #2


Honne Gowda A wrote:

Hi,
Can anybody clarify why destruction happens in reverse way?
for example A<-B<-C, when C's object is constructed, ctor calls will A(),B() and
C(), when this object is destroyed, sequence of calls will be ~C(),~B(),and
~C().
You mean ~A() :-)
I am wondering why cant' we destroy the object like ctor way?..Are there
any good reasons for this?


Generally it is most always a good idea to undo things in the exact reverese
order that they got done.

But in specific:
Suppose class C is dependent on things in A. For the construction this
is not a problem, since C is constructed after A has finished construction,
so at the time the ctor of C runs, it can access all of A's functionality.

But then destructin happens. Again C has to do work and wants to use information
from A. If A gets destructed first, then that information will not be available
any more. But if the destruction starts at C it can use everything from A needed
to do its job.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 19 '05 #3

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

Similar topics

6
by: tzach | last post by:
Call constructor on an already existing instance? I like to execute the constructor logic, including all the class member constructors, on an existing instance (with out executing the destructor)....
7
by: Robin Forster | last post by:
I have two classes: aule_gl_window (parent class) and aule_button (sub class) I want to call the super class (parent) constructor code from the sub class constructor.
23
by: Fabian Müller | last post by:
Hi all, my question is as follows: If have a class X and a class Y derived from X. Constructor of X is X(param1, param2) . Constructor of Y is Y(param1, ..., param4) .
8
by: JAL | last post by:
According to MSDN2, if a managed class throws an exception in the constructor, the destructor will be called. If an exception is thrown in the constructor the object never existed so how in the...
14
by: gurry | last post by:
Suppose there's a class A. There's another class called B which looks like this: class B { private: A a; public : B() { a.~A() } }
3
by: sarathy | last post by:
Hi all, I have doubt regarding how objects are passed in C++. The primary problem of passing by value in C++, is that the destructor of the object passed will be called twice, thus creating...
10
by: Szabolcs Horvát | last post by:
Consider the attached example program: an object of type 'A' is inserted into a 'map<int, Am;'. Why does 'm;' call the copy constructor of 'A' twice in addition to a constructor call? The...
12
by: Rahul | last post by:
Hi Everyone, I have the following code and i'm able to invoke the destructor explicitly but not the constructor. and i get a compile time error when i invoke the constructor, why is this so? ...
3
by: Rudi | last post by:
Hello, following problem: At program end or release an assembly a serial device should get a final exit sequence. How can I do this? With Dispose() it's no problem, but this assembly is used...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.