首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

请教这段程序在哪块调用拷贝构造函数的

2012-04-04 
请问这段程序在哪块调用拷贝构造函数的#includeiostream.h#includestring.hclass Student{public:Stud

请问这段程序在哪块调用拷贝构造函数的
#include<iostream.h>
#include<string.h>
class Student
{
public:
Student(char *pName="no name",int ssId=0)
{
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;
cout<<"拷贝构造函数"<<endl;
}
~Student()
{
cout<<"Destructing"<<name<<endl;
}
protected:
char name[40];
int id;
};
void fn(Student s)
{
cout<<"In function fn()\n";
}

void main()
{
Student randy("Randy",1234);
cout<<"Calling fn()\n";
fn(randy);
cout<<"Retruned from fn()\n";
}


[解决办法]

C/C++ code
void main(){Student randy("Randy",1234);cout<<"Calling fn()\n";fn(randy);  // 传递形参的时候要复制randy 调用拷贝构造函数!cout<<"Retruned from fn()\n";} 

热点排行