For reference, this is the output of a typical C++ compiler, after fixing all the errors int the program and adding a few spaces:
Constructing Person Amadis
Copying Amadis into its own block
Copying Amadis into its own block
Destructing Amadis
Constructing Person Kansas
Copying Kansas into its own block
Destructing Kansas
Destructing Kansas
Destructing Amadis
Destructing Amadis
|
(tested with GNU g++ on linux, and IBM xlc on aix. Also, online:
http://ideone.com/utZp5 )
Specifically, in the line
Person s=fun(p)
, you're doing the following:
1) passing a Person p to fun() by value. 1 copy ctor is called (line 2 of the output) and a temporary Person called "a" is created on fun()'s stack frame.
2) the function foo() creates a named temporary called "ms", and then returns it by value. This value is used to copy-construct the Person s inside main(). This is done in one step in C++: main's "s" and fun()'s "ms" are unified (occupy the same memory address) and only one copy-constructor is called (line 3 of the output), from a into ms/s.
3) The destructor for the temporary 'a' is called (line 4 of the output)
PS: there are many compilation errors in the program, try fixing them at
http://ideone.com