giles said:
"Mike Wahler" <
[email protected]> wrote in message
Have another query...
Contructor is used only to initialise the object and not to create it
in the memory isn't it?
That's the same thing. Before the ctor is invoked, memory for
the object is alloted (either as a static or automatic object, or
dynamically allocated). Then the constructor is called, which
'constructs' (a.k.a. initializes) the object.
When a contructor throws an exception the destructor for it is not
called. Why is this?
Because if the constructor does not complete, then the
object has not been constructed. You don't need a destructor
called for an object which hasn't been created.
However, if you allocate your object with operator 'new'
and the ctor throws, then the compiler is responsible for
ensuring that the allocated memory is freed. Note that
there's still no destructor call. A destructor is only
for 'cleaning up' a fully constructed object. An object
whose contstructor has thrown an exception is not such
an object.
The object is created in the memory,
Memory space is made for the object when it is allocated
or defined. No real 'object' exists yet at that point,
only 'raw' memory. It's the constructor that actually
'builds' ('creates') the object in that memory, i.e.
initializes the data members.
its just
that its not initialised
Right. There's no 'object' yet, only 'raw' memory.
so why shouldn't the destructor get called
Because there's nothing to destruct.
and how does the compiler know it shouldn't call the destructor?
The language definition dicates that. A compiler that
conforms to the language will 'know'.
-Mike