一道华为笔试题

#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.在声明一个对象时如果同时赋值,则调用拷贝构造,省掉一个默认构造。这点很重要。

转载于:https://www.cnblogs.com/skyofbitbit/p/4756072.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值