由一道面试题想到的——c++ constructor destructor的调用

王朝c/c++·作者佚名  2006-02-01
窄屏简体版  字體: |||超大  

昨天看到一个moto公司的面试题,题目如下:

#include <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

*/

#include <iostream>

#include <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

回过头来我们看看上面的例子:

#include <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;

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航