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;
}
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;
}
本文探讨了C++中不同类型的内存管理技术,包括自定义new和delete运算符的行为,以及如何使用模板特化来实现特定类型的对象构造。通过几个具体的代码示例,展示了这些技术的实际应用。
&spm=1001.2101.3001.5002&articleId=1654229&d=1&t=3&u=3afdeac87d7e407fbb36cf792942ddaf)
1759

被折叠的 条评论
为什么被折叠?



