对下面的一段代码的输出看不懂,求解释下#include <iostream>
#include<vector>
#include<deque>
#include<list>
#include<string>using namespace std;
class test{
public:
int a;
int *b;
test(int a){
this->a=a;
b=new int[5];
b[0]=11;
b[1]=12;
cout<<"construct a="<<a<<endl;
}
test(const test & t){
a=t.a+1;
b=new int[5];
cout<<"copy construct"<<"a="<<a<<endl;
}
~test(){
delete[]b;
cout<<"deconstruct"<<"a="<<a<<endl;
}};
int main(int argc,char**argv){
vector<test> te;
test aa(1);
te.push_back(aa);
cout<<"---"<<endl;
te.push_back(aa);
//te.push_back(a);
char tmp;
cin >>tmp;
return 0;
}C c++ 复制构造函数
#include<vector>
#include<deque>
#include<list>
#include<string>using namespace std;
class test{
public:
int a;
int *b;
test(int a){
this->a=a;
b=new int[5];
b[0]=11;
b[1]=12;
cout<<"construct a="<<a<<endl;
}
test(const test & t){
a=t.a+1;
b=new int[5];
cout<<"copy construct"<<"a="<<a<<endl;
}
~test(){
delete[]b;
cout<<"deconstruct"<<"a="<<a<<endl;
}};
int main(int argc,char**argv){
vector<test> te;
test aa(1);
te.push_back(aa);
cout<<"---"<<endl;
te.push_back(aa);
//te.push_back(a);
char tmp;
cin >>tmp;
return 0;
}C c++ 复制构造函数
解决方案 »
- 单文档模式,如何让窗口总在最前
- res 文件丢失
- HtmlHelp 调用.chm文件.页面可以跳转到具体某页.Contents的目录节点怎么不相应跳转?
- 关于VC中使用Office的问题求助
- 一个很怪异的问题
- 有没有哪本书介绍windows消息系统,特别是钩子(hook)函数比较清晰的?
- if ((strComp != _T (".")) && (strComp != _T (".."))) 中的(".")和("..")代表什么目录??
- ●●●调查:::有哪些大哥在自己的软件中用到BCG或者XTreme的???●●
- 局域网内的一台机器,如何能做服务器,让INTERNET上的机器访问?在线等。
- 江湖救急!!!!请大虾们帮忙!!!
- 小弟用mfc做了一个视频播放的ocx控件,一放到外部对话框上就会死掉还会弹出一个错误对话框。
- 哪位高人知道Quartus ii中怎么编译testbench ?
te.push_back(aa);调用拷贝构造函数
函数退出时调用析构函数啊输出是什么样的啊?
a=t.a+1;
每拷贝一次a增加1,所以完全正确。
a=t.a+1;
b=new int[5];
cout<<"copy construct"<<"a="<<a<<endl;
}
我认为如果输出a=3那么t.a==2,那么te.push_back(aa);中的aa.a==2 ,但是复制构造函数是常引用啊不会改变aa.a的值吧,能解释下么?