📄 passingbigstructures3.cpp
字号:
//: C11:PassingBigStructures.cpp
// From Thinking in C++, 2nd Edition
// Available at http://www.BruceEckel.com
// (c) Bruce Eckel 2000
// Copyright notice in Copyright.txt
#include <iostream>
#include <string>
using namespace std;
class Big{
public:
char buf[100];
int i;
long d;
Big(){}//缺省构造函数
//拷贝构造函数
Big(Big& b)
{
cout << "I am in Big(Big& b)." << endl;
i = b.i;
d = b.d;
::strcpy(buf,b.buf);
}
~Big()
{
cout << "I am in ~Big()." << endl;
}
};
/*
Big bigfun(Big temp) { //实参对象传递给形参对象时,调用拷贝初始化构造函数
temp.i = 3; // Do something to the argument
temp.d = 3.3;
::strcpy(temp.buf,"I am temp!");
return temp;//函数返回对象时,调用拷贝构造函数初始化一个匿名对象,在函数返回后该匿名对象作为函数返回值
}
*/
Big& bigfun(Big& temp) {
temp.i = 3; // Do something to the argument
temp.d = 3.3;
::strcpy(temp.buf,"I am temp!");
return temp;
}
void main()
{
Big b1, b2;
b1.i = 1;
b1.d = 1.1;
::strcpy(b1.buf,"I am b1!");
b2.i = 2;
b2.d = 2.2;
::strcpy(b2.buf,"I am b2!");
b2 = bigfun(b1);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -