kevin_qing:我找到了你的“决定提一个有点难度的问题,关于new/delete 的重载”的答案,嘿嘿^_^

vcmfc 2001-08-06 07:35:43
你的问题:http://www.csdn.net/expert/TopicView.asp?id=196640


答案:

在《C++ Primer》3/e繁体版P431页提到的可无具名的Namespace可能解决你的在一个.cpp里面使用某一个new/delete对,在另外一个.cpp里面使用另外一对,你可以在每个.cpp里使用如下代码:

//xxx.cpp

namespace{
operator new(size);
.............}

你使用new的代码.............



答案是不是正确你试一下。^_^

然后是你
...全文
234 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
seedundersnow 2001-08-06
  • 打赏
  • 举报
回复

我已经当 Virtual的FANS 很久拉~~~~~~~~~~~~

vcmfc 2001-08-06
  • 打赏
  • 举报
回复



楼上的,怎么你也成为Virtual的FANS????????????????????



seedundersnow 2001-08-06
  • 打赏
  • 举报
回复
up
vcmfc 2001-08-06
  • 打赏
  • 举报
回复
而且《C++ Primer》刚好对这部分讲得非常的少,且在《C++ programming language》3/e上也没有找到说明,看来还有研究呀!



我上面的错别字特别多呀!,请不要见怪。
vcmfc 2001-08-06
  • 打赏
  • 举报
回复
以下说明匿名namespace已经生效了,可是我把上面的代码改成new,delete后,编译出错,出错在匿名namespace里的new delete不明确,让我糊涂。



还是说cber等来看一下。
vcmfc 2001-08-06
  • 打赏
  • 举报
回复
你上面的代码可以编译,真让我奇怪呀!


这是我的一个示例:
19-1.cpp
---------------------------
#include <iostream>
#include <cstddef>
#include <malloc.h>
#include "19-2.h"
void k()
{cout<<"Global k"<<endl;}

int main(int argc, char *argv[])
{
k();
f();
return 0;
}

------------------------------
19-2.cpp
-------------------------------
#include <iostream>
#include <cstddef>
#include <malloc.h>
#include "19-2.h"
namespace{
void k()
{cout<<"19-2 k"<<endl;}
}
void f()
{
k();
}

---------------------------------
19-2.h
--------------------------------
void f();


结果输出是:
Global K;
19-2 k;
Kevin_qing 2001-08-06
  • 打赏
  • 举报
回复
好呀~
vcmfc 2001-08-06
  • 打赏
  • 举报
回复
晚上我试一下,明天给你回复。
Kevin_qing 2001-08-06
  • 打赏
  • 举报
回复
// ttt.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "stdio.h"
void* operator new(unsigned int s)
{
printf("global New from ttt.cpp\n");
return malloc(s);
}

void operator delete(void* p)
{
printf("global delete from ttt.cpp\n");
free(p);
return ;
}

namespace{
void* operator new(unsigned int s)
{
printf("New from ttt.cpp\n");
return malloc(s);
}
void operator delete(void* p)
{
printf("delete from ttt.cpp\n");
free(p);
return ;
}
void main1()
{
delete new char[1024];
}
};
void main()
{
delete new char[1024];
main1();
}


result:
D:\temp\ttt\Debug>ttt
global New from ttt.cpp
global delete from ttt.cpp
global New from ttt.cpp
global delete from ttt.cpp

D:\temp\ttt\Debug>_
Kevin_qing 2001-08-06
  • 打赏
  • 举报
回复
--------------------Configuration: ttt - Win32 Debug--------------------
Compiling...
ttt.cpp
c:\tools\microsoft visual studio\vc98\include\stdio.h(70) : fatal error C1001: INTERNAL COMPILER ERROR
(compiler file 'msc1.cpp', line 1794)
Please choose the Technical Support command on the Visual C++
Help menu, or open the Technical Support help file for more information
tttt1.cpp
c:\tools\microsoft visual studio\vc98\include\stdio.h(70) : fatal error C1001: INTERNAL COMPILER ERROR
(compiler file 'msc1.cpp', line 1794)
Please choose the Technical Support command on the Visual C++
Help menu, or open the Technical Support help file for more information
Generating Code...
Error executing cl.exe.

ttt.exe - 2 error(s), 0 warning(s)
Kevin_qing 2001-08-06
  • 打赏
  • 举报
回复
咦?
试试,呵呵~~~~~~~
vcmfc 2001-08-06
  • 打赏
  • 举报
回复
他还没有看到!!!


up
wjyasd 2001-08-06
  • 打赏
  • 举报
回复
^&^
seedundersnow 2001-08-06
  • 打赏
  • 举报
回复
我是来偷听的
vcmfc 2001-08-06
  • 打赏
  • 举报
回复



那就留给老兄你去研究呀!,我只能帮到这里了。




结贴........................................^_^
vcmfc 2001-08-06
  • 打赏
  • 举报
回复
黄森堂,它是......................我的真实姓名。
Kevin_qing 2001-08-06
  • 打赏
  • 举报
回复
我的代码编译起来满好的,就是没有效果
seedundersnow 2001-08-06
  • 打赏
  • 举报
回复

黄森堂 是谁?

vcmfc 2001-08-06
  • 打赏
  • 举报
回复
Up

16,472

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧