由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - 问个overloading new operator的问题
相关主题
An object of A automatically converted to an object of B.为啥gcc找不到类的构造函数?
why use static function here?一个c++小问题
question overloading ++ error问个c++的template的问题
请问关于overloading <<string operator +
ambiguous operators in c++member and friend
why copy assignment operator returns non-const type?C++ operator = overloading用copy & swap有啥优点
operator() overloading 一问请问一个implicit conversion的问题(C++)
为什么在overloading中,friend <<不能读取private值呢?一个inheritance 的问题
相关话题的讨论汇总
话题: new话题: operator话题: mempool话题: mynew话题: size
进入Programming版参与讨论
1 (共1页)
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,别问我, 我也是遵从别人的数据类型.

1 (共1页)
进入Programming版参与讨论
相关主题
一个inheritance 的问题ambiguous operators in c++
one question about overloading operator deletewhy copy assignment operator returns non-const type?
这段code有啥问题?operator() overloading 一问
How to overload global new operator?为什么在overloading中,friend <<不能读取private值呢?
An object of A automatically converted to an object of B.为啥gcc找不到类的构造函数?
why use static function here?一个c++小问题
question overloading ++ error问个c++的template的问题
请问关于overloading <<string operator +
相关话题的讨论汇总
话题: new话题: operator话题: mempool话题: mynew话题: size