📄 demo_copy_constructor_7.cpp
字号:
//************************************************
// 直接调用构造函数产生无名对象
//************************************************
# include <iostream.h>
# include <string.h>
class Student
{
public:
Student(char* pName="No Name")
{
cout<<"Constructing new student "<<pName<<endl;
strcpy(name,pName);
}
~Student()
{
cout<<"Destructing "<<name<<endl;
}
protected:
char name[20];
};
void fn(Student& s)
{
cout<<"In function fn()"<<endl;
}
void main()
{
Student& refs=Student("Randy"); //创建无名对象初始化引用
// Student s=Student("Jenny"); //创建无名对象初始化对象
// Student s="Jenny";
Student s("Jenny");
fn(Student("Danny")); //创建无名对象作为实参传递给形参
//构造函数用于强制类型转换
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -