一个moto公司的面试题

昨天看到一个moto公司的面试题,题目如下:
#i nclude <iostream>
Using namespace std ;
class human
{
public:
human()
{
 human_num++;
}
static int human_num;  
~human()
{
human_num--;
print();
}
void print()
{
cout<<"human nun is: "<<human_num<<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;
}
写出程序的输出结果。。。。。只 知道输出5行,但是不知道每行 human_num的值是多少??
我们先不看这个题目,我们先来了解一下构造函数在按值传向函数,以及按值从函数返回的时候会发生什么:
1、将对象按值传给函数的时候,首先会创建该对象的一个副本,将这个副本传给函数,但是在创建这个副本的时候,不会调用构造函数,而在这个函数返回的时候会调用析构函数。
2、函数按值返回对象的时候,也会创建返回对象的一个副本,同样的也不会调用构造函数,在返回后调用析构函数销毁该对象。
我们先来看下面的一个程序:
/*
 Constructor and destructor
*/
#i nclude <iostream>
#i nclude <string>
using namespace std ;
class test
{
 public:
  //Constructor
  test( string s ):str(s)
  {
   cout << "Constructing " << endl ;
  }
  
  //Destructor
  ~test()
  {
   cout << "Destructing " << endl ;
  }
  
  string getValue()
  {
   return str ;
  }
 private:
  string str ;
} ;
void display( test t )
{
 cout << t.getValue() << endl ;
}
test getTest()
{
 return test("Jun") ;
}
void main()
{
 //这里很显然调用了一次构造函数,程序会输出:constructing
 test te = test("Guo") ; 

//在向函数按值传对象的时候,会新建传递的对象的副本
//但是此时不会调用构造函数。不会有constructing输出

 display(te) ;
 //在函数返回的时候,会调用其destructor销毁刚才的副本 
 //程序输出:Destructing
 getTest() ; 
 //在getTest里面我们新建了一个对象,因此会有一次constructor的调用
 //程序输出:constructing ,我们知道在程序返回的时候,会生成一个副本返回
 //因此实际上在函数里面会有destructor的调用,然后再返回到main函数里面
 //的对象也会被destructed,因此会再次输出destructing,最后销毁最初的
//对象。

}
//最终程序的结果:
//constructing
//Destructing
//Guo
//destructing
//constructing
//destructing
//destructing
//destructing
回过头来我们看看上面的例子:
#i nclude <iostream>
Using namespace std ;
class human
{
public:
human()
{
 human_num++;
}
static int human_num;
~human()
{
human_num--;
print();
}
void print()
{
cout<<"human nun is: "<<human_num<<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被构造,human_num = 1
h1.print();
//h1作一个拷贝,然后将拷贝传入函数,没有构造函数被调用,因此会输出1
//返回的是一个对象,因此还是会做一个拷贝,然后返回,此时调用一次溪沟函数,临时拷贝被析构,human_num=0 ,于是h2.print(),同样输出0,再h1,h2被析构,分别输出-1,-2
human h2 = f1(h1);
h2.print();
return 0;
}
maleo 发表于 2005-11-23 14:53:00
 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值