the copy constructor's running

When I run this program, some thing happen that I can't understand, especially this sentence: Person s=fun(p) in the main function, please tell me the details of this sentence's operation process. I can not understand that why there will have two rather than three sentences:Destructing Amadis! when this sentence run to completion. Thanks for any helps!
#include<iostream>
using namespace std;
class Person
{
public:
Person(char *pN="no name");
Person(Person &p);
~Person();
private:
char *pName;
};

Person::Person(char *pN)
{
cout<<"Constructing Person"<<pN<<endl;
pName = new char[strlen(pN)+1];
if(pName!=0)
strcpy(pName, pN);
}

Person::Person(Person &p)
{
cout<<"Copting"<<p.pName<<"into its own block\n";
pName=new char[strlen(p.pName)+1];
if(pName!=0)
strcpy(pName, p.pName);
}

Person::~Person()
{
cout<<"Destructing"<<pName<<endl;
delete pName;
}

Person fun(Person a)
{
Person ms(a);
return ms;
}

void main()
{
Person p("Amadis");
Person s = fun(p);
Person ms("Kansas");
Person ns = ms;
}

There are three destructors for "Amadis" are being called, and three sentences are being displayed. The first one is the fourth sentence down, this is being called when the program exists the function "fun()". If you aren't seeing them all then add #include<stdlib.h> to the top of your file and add a function like this somewhere near the top
1
2
3
4
5
void pause()
{
   cin.sync();
   cin.ignore();
}

then at the beginning of main enter:
 
atexit(pause);

This will hold your console open until the absolute end of execution.
Last edited on
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
Last edited on
I have to ask a question. In the sentence: Person s=fun(p), dose this operator '=' really called the function as follows:
Person::Person(Person &p)
{
cout<<"Copting"<<p.pName<<"into its own block\n";
pName=new char[strlen(p.pName)+1];
if(pName!=0)
strcpy(pName, p.pName);
}
If the operator can't call this function, what can it do, and what's its meaning?
Person s=fun(p) calls the copy constructor and not the copy assignment operator because 's' is being constructed on that line.

It can be written like
Person s(p);
or
Person s=fun(p);
or
Person s(fun(p));

They all result in 's' copy constructor.

Note: A copy constructor is being called for the parameter being passed to 'fun' by value, as well as being returned from 'fun' by value.
Topic archived. No new replies allowed.