请帮我看看这个拷贝构造函数!
ztwz 2004-09-24 03:10:49 //问题我都用注释写在相应的位置了!小弟先谢啦!
#include <iostream>
#include <string.h>
using namespace std;
class student
{
public:
student(char * pname="no name", int ssid=0)
{
id=ssid;
strcpy(name,pname);
cout<<"constrcting new student"<<pname<<endl;
}
student(student&s) //这个student &s是不是复制student s这个构造的地址!这个函数其本看不懂在做什么,请大虾解释一下啦!
{
cout <<"constructing copy of"<<s.name<<endl;
strcpy(name,"copy of");
strcat(name,s.name);
id=s.id;
}
~student()
{
cout<<"destructing"<<name<<endl;
}
protected:
char name[40];
int id;
};
void fn(student s) //这儿是用student s这个构造函数的值做参数吗?构造函数不是没反回值的吗?在这要执行完student s这个构造函数才进入fn()这个函数主体吧!
{
cout<<"in function fn()"<<endl;
}
int main(int argc, char *argv[])
{
student randy("randy",1234);
cout<<"calling fn()"<<endl;
fn(randy);
cout<<"returned from fn()\n";
system("PAUSE");
return 0;
}