强化训练2--匿名对象的生命周期
2017-02-15 10:33
197 查看
//临时对象和匿名对象没有人去接它,编译器马上就析构了
#include<iostream>
using namespace std;
class ABCD
{
public:
ABCD(int a,int b,int c)
{
this->a = a;
this->b = b;
this->c = c;
printf("ABCD() construct,a:%d,b:%d,c:%d \n",this->a,this->b,this->c);
}
~ABCD()
{
printf("~ABCD() construct,a:%d,b:%d,c:%d \n",this->a,this->b,this->c);
}
int getA()
{
return this->a;
}
protected:
private:
int a;
int b;
int c;
};
class MyE
{
public:
MyE():abcd1(1,2,3),abcd2(4,5,6),m(100)
{
cout << "MyD()"<<endl;
}
~MyE()
{
cout << "~MyD()"<<endl;
}
MyE(const MyE &obj):abcd1(7,8,9),abcd2(10,11,12),m(100)
{
printf("MyD(const MyD & obj)\n");
}
protected:
private:
ABCD abcd1;//c++编译器不知道如何构造abcd1;先执行这个函数的构造函数
ABCD abcd2;
const int m;
};
int doThing(MyE mye1)
{
//printf("doThing() mye1,abcd1,a:%d \n",mye1.abcd1.getA());//有问题一直通过不了
printf("doThing() mye1,abcd1,a:%d \n");
return 0;
}
int run3()
{
printf("run3 star..\n");
ABCD(400,500,600);//临时对象的生命周期,直接调用ABCD类的构造函数
//临时对象和匿名对象没有人去接它,编译器马上就析构了printf("run3 end..\n");
return 0;
}
int main()
{
run3();
system("pause");
return 0;
}
匿名对象转正
#include<iostream>
using namespace std;
class ABCD
{
public:
ABCD(int a,int b,int c)
{
this->a = a;
this->b = b;
this->c = c;
printf("ABCD() construct,a:%d,b:%d,c:%d \n",this->a,this->b,this->c);
}
~ABCD()
{
printf("~ABCD() construct,a:%d,b:%d,c:%d \n",this->a,this->b,this->c);
}
int getA()
{
return this->a;
}
protected:
private:
int a;
int b;
int c;
};
class MyE
{
public:
MyE():abcd1(1,2,3),abcd2(4,5,6),m(100)
{
cout << "MyD()"<<endl;
}
~MyE()
{
cout << "~MyD()"<<endl;
}
MyE(const MyE &obj):abcd1(7,8,9),abcd2(10,11,12),m(100)
{
printf("MyD(const MyD & obj)\n");
}
protected:
private:
ABCD abcd1;//c++编译器不知道如何构造abcd1;先执行这个函数的构造函数
ABCD abcd2;
const int m;
};
int doThing(MyE mye1)
{
//printf("doThing() mye1,abcd1,a:%d \n",mye1.abcd1.getA());//有问题一直通过不了
printf("doThing() mye1,abcd1,a:%d \n");
return 0;
}
/*int run3()
{
printf("run3 star..\n");
//这个时候没有创建一个对象
ABCD(400,500,600);//临时对象的生命周期,直接调用ABCD类的构造函数
//临时对象和匿名对象没有人去接它,编译器马上就析构了printf("run3 end..\n");
return 0;
}*/
int run3()
{
printf("run3 star..\n");
//这个时候创建一个对象
ABCD abcd=ABCD(400,500,600);
//有对象去接了
printf("run3 end..\n");
return 0;
}
int main()
{
run3();
system("pause");
return 0;
}
相关文章推荐
- 对象的打印和比较对象以及匿名对象和对象的生命周期
- 匿名对象的生命周期
- Java的匿名对象的创建和生命周期
- Java面向对象-打印和比较对象-对象生命周期-匿名对象
- Java的匿名对象的创建,生命周期和应用场景
- C++ 匿名对象的生命周期
- hibernate中持久化对象的生命周期(转载)
- JAVA之旅(四)——面向对象思想,成员/局部变量,匿名对象,封装 , private,构造方法,构造代码块
- Java 对象锁-synchronized()与线程的状态与生命周期与守护进程
- 强化基础-JS对象中数组常用的属性和方法
- 在对象生命周期内只执行一次的Block
- Mybatis核心对象及生命周期
- 深入探索CORBA对象生命周期之慨述-Java基础-Java-编程开发
- session对象生命周期
- OS视图控制对象生命周期-init、viewDidLoad、viewWillAppear、viewDidAppear、viewWillDisappear、view
- C++:析构函数、对象生命周期、类型转换构造、拷贝构造、拷贝赋值、深浅拷贝
- 对象生命周期
- [翻译] 编写高性能 .NET 代码--第二章 GC -- 减少分配率, 最重要的规则,缩短对象的生命周期,减少对象层次的深度,减少对象之间的引用,避免钉住对象(Pinning)
- C++中匿名对象应当是一个左值
- 3000门徒内部训练绝密视频(泄密版)第2课:Scala面向对象彻底精通及Spark源码阅读