C++ 匿名对象产生场景
2016-06-29 10:37
316 查看
//匿名对象产生的三种场景 #include<iostream> using namespace std; class Point{ public: Point(int a,int b){ cout << "有参构造函数被调用了1" << endl; this->x = a; this->y = b; } Point(Point &a1){ cout << "拷贝构造函数被调用了2" << endl; this->x = a1.x; this->y = a1.y; } ~Point(){ cout << "析构函数被调用了3" << endl; cout << "x=" << x << endl; cout << "y=" << y << endl; } Point Protset(int a){ this->x = a; return *this; //执行 return *this; 会产生一个匿名对象,作为返回值 //强调:如果返回值是引用,则不会产生匿名对象 } Point Protset2(int a){ Point temp(a, a); return temp; //执行 return temp;会先产生一个匿名对象,执行拷贝构造函数,作为返回值, //然后释放临时对象temp } //总结:函数返回值为一个对象(非引用)的时候会产生一个匿名对象,匿名对象根据主函数的操作决定生命周期 Point& Protset3(int a){ Point temp(a, a); return temp; //执行 return temp;不会产生匿名对象,而是会将temp的地址先赋值到引用中, //在释放temp的内存,此时Point&得到是一个脏数据 } void PrintfA()const{ cout << "x="<<x << endl; cout << "y=" << y << endl; } private: int x; int y; }; void ProtectA(){ //生成一个匿名对象,因为用来初始化另一个同类型的对象,这个匿名对象会直接转换成新的对象, //减少资源消耗 Point p1 = Point(1,1); /*Point p2(2, 2); p2 = p1.Protset(3); p2.PrintfA();*/ //观察发现p2打印出正确数据,因此得出结论p1.Protset(3);返回来一个匿名对象, //但是这个匿名对象执行完"="之后,才会被释放 Point p4(5, 5); p4=p1.Protset2(4); p4.PrintfA(); } void main(){ ProtectA(); system("pause"); }
相关文章推荐
- Google C++单元测试框架(Gtest)系列教程之一——入门
- leetcode #151 in cpp
- 【6-29】关于 C++ 框架、库和资源的一些汇总列表(转)
- 数组与指针(二)
- 数组与指针(一)
- C++何时会帮忙合成default constructor
- C++ 虚函数表解析
- c++之封装
- 彻底搞清 C/C++中a++与++a的区别
- 使用MinGW编译Boost
- 使用Boost的DLL库管理动态链接库
- c++运算符的重载
- C++中32位单精度浮点数有效数字是多少位?
- c++ string 和封装上
- 树状数组 Binary Indexed Tree
- 提高项目12.5-数组大折腾
- c语言之跨平台的文件拷贝filecopy
- 提高项目12.4-数组大折腾
- C++之操作符重载和友元学习笔记
- 提高项目12.2数组大折腾