快捷搜索:   服务器  安全  linux 安全  MYSQL  dedecms

程序员必知:C/C++面试题(3)

    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;
}

顶(0)
踩(0)

您可能还会对下面的文章感兴趣:

最新评论