4道稍微高级点的c++面试题,供菜鸟一窥门径
4道稍微高级点的c++面试题,供初学者一窥门径
1.写出下面程序的输出
class abc;
void del(abc *pobj){
delete pobj;
}
class abc{
public:
abc(){
printf( "abc\r\n ");
}
~abc(){
printf( "~abc\r\n ");
}
};
int main()
{
abc *pobj = new abc;
del(pobj);
}
2.写出下面程序的输出
void* operator new(size_t size)
{
printf( "malloc %u\r\n ", size);
return malloc(size);
}
void operator delete(void *memblock){
printf( "free\r\n ");
return free(memblock);
}
class abc{
public:
abc(){
printf( "abc\r\n ");
throw int();
}
~abc(){
printf( "~abc\r\n ");
}
};
int main(){
try{
new abc;
}catch(int& i){
printf( "%d\r\n ", i);
}
return 0;
}
3.写出下面程序的输出
template <typename T>
class abc{
public:
abc(){
printf( "primary\r\n ");
}
};
template <>
abc <int> ::abc(){
printf( "member spec\r\n ");
};
template <typename T, typename P>
class abc <T (*)(P)> {
public:
abc(){
printf( "partial spec\r\n ");
}
};
int main()
{
abc <void* (*)(int)> f_abc;
abc <int> i_abc;
}
4.下面的代码能否通过编译?为什么
class a{
public:
virtual ~a(){
}
private:
void operator delete(void *p);
};
int main()
{
a _1;
}
------解决方案--------------------
1 abc
2 malloc 40
malloc 30
malloc 1
abc
free
0
free
free
------解决方案--------------------
1.abc
但是不是很明白
2.malloc 1
abc
free
0
考察的是重载operator new和delete,以及类内存分布,异常
3.partial spec
member spec
这题猜的,这样的模板方法没见过
4.不能,会默认去调delete,结果没有
------解决方案--------------------
4.不是没有,是private的,写错
------解决方案--------------------
先回答,再验证。
1、
abc,出错!
2、
malloc 4
abc
0
3、
partial spec
member spec
4、
可以,栈变量,构造、析构函数均可访问。
------解决方案--------------------
其他的没什么好说的, 不过我觉得第四题的答案可能是不确定的, 可以在VC6中试试.
有时你重载了一个operator new, 不管你有没有调用, 一运行程序, 就会崩溃.
可能是因为不仅仅你自己的代码调用了你的operator new, 而且编译器自已嵌入进来的代码也调用了, 而这时异常发生了.
------解决方案--------------------
弱弱的问一下, 为什么第一题不是输出:abc, ~abc
而是报错?
------解决方案--------------------
1.写出下面程序的输出
class abc;
void del(abc *pobj){
delete pobj;
}
class abc{
public:
abc(){
printf( "abc\r\n ");
}
~abc(){
printf( "~abc\r\n ");
}
};
int main()
{
abc *pobj = new abc;
del(pobj);
}
2.写出下面程序的输出
void* operator new(size_t size)
{
printf( "malloc %u\r\n ", size);
return malloc(size);
}
void operator delete(void *memblock){
printf( "free\r\n ");
return free(memblock);
}
class abc{
public:
abc(){
printf( "abc\r\n ");
throw int();
}
~abc(){
printf( "~abc\r\n ");
}
};
int main(){
try{
new abc;
}catch(int& i){
printf( "%d\r\n ", i);
}
return 0;
}
3.写出下面程序的输出
template <typename T>
class abc{
public:
abc(){
printf( "primary\r\n ");
}
};
template <>
abc <int> ::abc(){
printf( "member spec\r\n ");
};
template <typename T, typename P>
class abc <T (*)(P)> {
public:
abc(){
printf( "partial spec\r\n ");
}
};
int main()
{
abc <void* (*)(int)> f_abc;
abc <int> i_abc;
}
4.下面的代码能否通过编译?为什么
class a{
public:
virtual ~a(){
}
private:
void operator delete(void *p);
};
int main()
{
a _1;
}
------解决方案--------------------
1 abc
2 malloc 40
malloc 30
malloc 1
abc
free
0
free
free
------解决方案--------------------
1.abc
但是不是很明白
2.malloc 1
abc
free
0
考察的是重载operator new和delete,以及类内存分布,异常
3.partial spec
member spec
这题猜的,这样的模板方法没见过
4.不能,会默认去调delete,结果没有
------解决方案--------------------
4.不是没有,是private的,写错
------解决方案--------------------
先回答,再验证。
1、
abc,出错!
2、
malloc 4
abc
0
3、
partial spec
member spec
4、
可以,栈变量,构造、析构函数均可访问。
------解决方案--------------------
其他的没什么好说的, 不过我觉得第四题的答案可能是不确定的, 可以在VC6中试试.
有时你重载了一个operator new, 不管你有没有调用, 一运行程序, 就会崩溃.
可能是因为不仅仅你自己的代码调用了你的operator new, 而且编译器自已嵌入进来的代码也调用了, 而这时异常发生了.
------解决方案--------------------
弱弱的问一下, 为什么第一题不是输出:abc, ~abc
而是报错?
------解决方案--------------------