#include<iostream>

using namespace std;

class numbered
{
private:static int seq;
public:
	numbered(){ mysn = seq++; }
	numbered(const numbered&n){ mysn = seq++; }
	int mysn;
};

int numbered::seq = 0;

void f(const numbered &s)
{
	cout << s.mysn << endl;
}
//void f(const numbered s)
//{
//	cout << s.mysn << endl;
//}

int main(){
	numbered a, b = a, c = b;
	f(a); f(b); f(c);//结果为1,2,3。f里面参数传递的不是类对象而是类对象的引用,不再触发拷贝构造函数;传非引用时,numbered a调用一次构造函数,f(a)还要调用一次拷贝构造函数
	system("pause");
	return 0;

}

  

相关文章:

  • 2022-12-23
  • 2021-07-06
  • 2021-09-20
  • 2021-04-29
  • 2021-06-19
  • 2021-08-05
  • 2022-12-23
猜你喜欢
  • 2022-01-08
  • 2022-12-23
  • 2022-01-05
  • 2021-06-09
  • 2022-12-23
相关资源
相似解决方案