一道华为笔试题
2015-08-24 22:08
281 查看
#include <iostream>
class human
{
public:
static int human_num;
human(){
human_num++;
std::cout<<"default human num is: "<<human_num<<std::endl;
};
human(const human& c){
std::cout<<"copy human num is: "<<human_num<<std::endl;
};
human operator=(const human& c){
std::cout<<"= human num is: "<<human_num<<std::endl;
int a = 2;
return c;
}
~human(){
human_num--;
std::cout<<"destruct human num is: "<<human_num<<std::endl;
}
void print()
{
std::cout<<"human num is: "<<human_num<<std::endl;
}
protected:
private:
};
int human::human_num = 0;
human& f1(human& x)
{
x.print();
return x;
}
int main(int argc, char* argv[])
{
human h1;
h1.print();
human h2 = f1(h1);
h2.print();
return 0;
}
//注意两点:1.函数临时返回对象就是传入的实参旳副本,直到拷贝给其他对象,再销毁这个临时对象。如果不拷贝则会立即销毁。2.在声明一个对象时如果同时赋值,则调用拷贝构造,省掉一个默认构造。这点很重要。
class human
{
public:
static int human_num;
human(){
human_num++;
std::cout<<"default human num is: "<<human_num<<std::endl;
};
human(const human& c){
std::cout<<"copy human num is: "<<human_num<<std::endl;
};
human operator=(const human& c){
std::cout<<"= human num is: "<<human_num<<std::endl;
int a = 2;
return c;
}
~human(){
human_num--;
std::cout<<"destruct human num is: "<<human_num<<std::endl;
}
void print()
{
std::cout<<"human num is: "<<human_num<<std::endl;
}
protected:
private:
};
int human::human_num = 0;
human& f1(human& x)
{
x.print();
return x;
}
int main(int argc, char* argv[])
{
human h1;
h1.print();
human h2 = f1(h1);
h2.print();
return 0;
}
//注意两点:1.函数临时返回对象就是传入的实参旳副本,直到拷贝给其他对象,再销毁这个临时对象。如果不拷贝则会立即销毁。2.在声明一个对象时如果同时赋值,则调用拷贝构造,省掉一个默认构造。这点很重要。
相关文章推荐
- 总结系列--Android开发规范
- MySQL数据库的常用命令语句记录——数据库及表模式语句
- UVA 658 It's not a Bug, it's a Feature!
- Note For Linux By Jes(7)-学习 shell scripts
- 技术博客是程序猿的不归路。。。
- UIEvent
- 读书笔记之linux/unix系统编程手册(43)
- 集合的基础知识
- MySQL数据库的常用命令语句记录——数据操纵语句及函数
- 信号量例子
- 各浏览器对 document、document.body、document.documentElement 对象的 onscroll 事件支持情况
- Linux的NTP配置总结(转)
- HDU 4280 Island Transport 又一发SAP
- ASCII码(转)
- 追求内心的自由
- QT中QWidget、QDialog及QMainWindow的区别
- 临近的最小数
- Maven安装与Maven HelloWorld入门实例
- Linux NetHogs监控工具介绍(转)
- group by和order by连用