Let use be a little careful here about the sequence of events.
First off, if you examine the assembler/machine code that is 100% exactly what is going on. However, (a) that may mean that your compiler is being "smart" (b) the standard says what the effect should be. So now we will discuss what actually should happen:
So consider your example. First if the code is written as you have written it.
(a) The object is created
(b) then the code in { } below the constructor is called
(c) the delete operator is called
(d) the program exits.
Now let us write a better example:
class Example
{
private:
int x;
double d;
MyObj MA;
MyObj MB;
public:
Example() : x(5),MA(4,5)
{
MB=MA;
}
};
int main()
{ Example A; }
Now what happens (assuming MyObj is definded elsewhere).
(a) x is initialized with the value 5.
(b) MA is constructed (calling the constructor with two values 4 and 5.
(c) MB is constructed (calling the constructor with no value e.g
MB()
(d) Then the assignment operator (=) is called on the previously constructed MB e.g.
MB.operator=(MA);
(e) the destructor for MB is called
(f) the destructor for MA is called
(g) memory for object A is marked free
e,f,g are the effect of the default destructor begin called.
Does that help / make it clear ??
It is maybe worth playing with the class but add destructor,constructors, copy constructors and assignment operators that printout their name etc.
print out in