- 论坛徽章:
- 0
|
#include <iostream.h>
#include <string.h>
class Student
{
public:
Student(char *pName="no name",int ssId=0)
{
id=ssId;
strcpy(name,pName);
cout<<"Constructing new student "<<pName<<endl;
}
Student(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;
};
class Tutor
{
public:
Tutor(Student& s):student(s)
{
cout<<"Constructing tutor\n";
}
protected:
Student student;
};
void fn(Tutor tutor)
{
cout<<"In function fn()\n";
}
void main()
{
Student randy("Randy",1234);
Tutor tutor(randy);
cout<<"Calling fn()\n";
fn(tutor);//这句不懂
cout<<"Returned from fn()\n";
}
书上说到fn(tutor);这句时应该调用Tutor的默认拷贝构造函数,但用VC进行单步跟踪时发现程序执行到这句时是调用了STUDENT类的拷贝构造函数,不懂啊!!!!高手讲讲好吗??!!
[ 本帖最后由 czlt 于 2006-9-4 21:32 编辑 ] |
|