求助...面试时遇到题目不知如何下手请各位大哥大姐帮帮忙
求助:面试题...有代码段,请说明终端输出内容 (假设:相应的库文件都已正确包含,且无其它输出语句) ,并指出其中的缺陷。
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;
} 工夫要下在平常哦,急来抱佛脚怎么行啊 :loveliness:
紧听教诲~! 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。
页:
[1]