p****s 发帖数: 32405 | 1 在C++里,
我尝试着自己定义一个new operator,
首先我这么来一把搞一个带三个参数的new:
#define mynew new (name, type, align)
然后我overload new 和new[]:
void *operator new(size_t size, const char *ModName, memPool_t *memType, size_t alignment) throw();
new[]的overload类似于上.
然后在code里call
pool = mynew(modName, memType, alignment) myPool;
(myPool为自定义的class)
编译的时候出错,compiler不认我定义的这个operator,说
undefined reference to `operator new(unsigned int,char const*,memPool*,unsigned int).
为啥不认呢? 不会是因为我自定义带三个参数的mynew类型和operator new里四个arg
相冲突吧? | p****s 发帖数: 32405 | 2 嗯, 莫非是我提问的技巧太烂...
再补充一下, new operator 我在myPool里也已经做了implementation,
无非是:
inline void *myPool::operator new(size_t size, const char*ModName, memPool_t *memType, size_t aignment) {
return malloc(size);
}
【在 p****s 的大作中提到】 : 在C++里, : 我尝试着自己定义一个new operator, : 首先我这么来一把搞一个带三个参数的new: : #define mynew new (name, type, align) : 然后我overload new 和new[]: : void *operator new(size_t size, const char *ModName, memPool_t *memType, size_t alignment) throw(); : new[]的overload类似于上. : 然后在code里call : pool = mynew(modName, memType, alignment) myPool; : (myPool为自定义的class)
| t****t 发帖数: 6806 | 3 i have no problem compiling the following program:
class mempool {};
void * operator new (size_t s, const char *, mempool*, size_t a) throw()
{
return operator new (s);
}
int main()
{
mempool* a=new("abc", NULL, 10) mempool;
}
BTW did you define ur macro literally like that? it will have some problem.
_t *memType, size_t aignment) {
【在 p****s 的大作中提到】 : 嗯, 莫非是我提问的技巧太烂... : 再补充一下, new operator 我在myPool里也已经做了implementation, : 无非是: : inline void *myPool::operator new(size_t size, const char*ModName, memPool_t *memType, size_t aignment) { : return malloc(size); : }
| p****s 发帖数: 32405 | 4 哦? By saying literally... 难道我前面哪里打错了什么字?
先从逻辑上说吧, 我的构架和方法是对的?
【在 t****t 的大作中提到】 : i have no problem compiling the following program: : class mempool {}; : void * operator new (size_t s, const char *, mempool*, size_t a) throw() : { : return operator new (s); : } : int main() : { : mempool* a=new("abc", NULL, 10) mempool; : }
| t****t 发帖数: 6806 | 5 the method sounds ok to me.
#define mynew new(a, b, c)
that's what you wrote
shouldn't it be
#define mynew(a,b,c) new(a,b,c)
but what's the point of defining the macro?
【在 p****s 的大作中提到】 : 哦? By saying literally... 难道我前面哪里打错了什么字? : 先从逻辑上说吧, 我的构架和方法是对的?
| p****s 发帖数: 32405 | 6 Hmm, 事实上我写了两个,
#define mynew1(name) new(name)
#define mynew2(size,type,align) new(size,type,align)
至于为什么要定义这样的new macro,别问我, 我也是遵从别人的数据类型.
【在 t****t 的大作中提到】 : the method sounds ok to me. : #define mynew new(a, b, c) : that's what you wrote : shouldn't it be : #define mynew(a,b,c) new(a,b,c) : but what's the point of defining the macro?
| t****t 发帖数: 6806 | 7 isn't it stupid...why don't you just write
#define mynew1 new
#define mynew2 new
or why don't you just use new instead of mynew1...
【在 p****s 的大作中提到】 : Hmm, 事实上我写了两个, : #define mynew1(name) new(name) : #define mynew2(size,type,align) new(size,type,align) : 至于为什么要定义这样的new macro,别问我, 我也是遵从别人的数据类型.
|
|