51Testing软件测试论坛
标题:
求助...面试时遇到题目不知如何下手请各位大哥大姐帮帮忙
[打印本页]
作者:
晓诺
时间:
2008-1-15 16:31
标题:
求助...面试时遇到题目不知如何下手请各位大哥大姐帮帮忙
求助:面试题...
有代码段,请说明终端输出内容 (假设:相应的库文件都已正确包含,且无其它输出语句) ,并指出其中的缺陷。
class Test
{
public:
Test( void )
{
std::cout << “Test constructed in default mode” << std::endl;
}
Test( int t )
{
std::cout << “Test constructed with arguments” << std::endl;
}
~Test( void )
{
std::cout << “Test destructed” << std::endl;
}
};
int main( int argc,char *argv[] )
{
Test *aa_ = new Test( 10 );
delete aa_;
Test *bb_ = malloc( sizeof(Test) );
free( bb_ );
Test *cc_ = malloc( sizeof(Test) );
new (cc_) Test( );
free( cc_ );
return 0;
}
作者:
red-hat
时间:
2008-1-15 18:37
工夫要下在平常哦,急来抱佛脚怎么行啊
作者:
晓诺
时间:
2008-1-16 10:03
紧听教诲~!
作者:
wgx198302
时间:
2008-1-18 16:36
class Test
{
public:
Test( void )
{
std::cout << "Test constructed in default mode" << std::endl;
}
Test( int t )
{
std::cout << "Test constructed with arguments" << std::endl;
}
~Test( void )
{
std::cout << "Test destructed" << std::endl;
}
};
int main( int argc,char *argv[] )
{
Test *aa_ = new Test( 10 );
delete aa_;
Test *bb_ = (Test *)malloc( sizeof(Test) );
free( bb_ );
Test *cc_ = (Test *)malloc( sizeof(Test) );
new (cc_) Test( );
free( cc_ );
return 0;
}
结果:
Test constructed with arguments
Test destructed
Test constructed in default mode
光用maloc/free无法满足动态对象的要求,不能够把执行构造函数和析构函数的任务强加于malloc/free。
欢迎光临 51Testing软件测试论坛 (http://bbs.51testing.com/)
Powered by Discuz! X3.2