c**a 发帖数: 316 | 1 比如 如果operator new不是static的,
A* p = new A();
是那个 A的实体调用了 operator new 呢? |
t****t 发帖数: 6806 | 2 你问得很好啊, 是那个A的对象调用了operator new呢?
【在 c**a 的大作中提到】 : 比如 如果operator new不是static的, : A* p = new A(); : 是那个 A的实体调用了 operator new 呢?
|
s***e 发帖数: 122 | 3 不是很熟悉这种比较深的东西,不过我的理解是,非static的成员函数都事实上隐含了
第一个参数this,所以它的参数应该就跟应该重载的operator new不match了吧。
【在 c**a 的大作中提到】 : 比如 如果operator new不是static的, : A* p = new A(); : 是那个 A的实体调用了 operator new 呢?
|
c**a 发帖数: 316 | 4 ...
都可以编译通过的。。。。
【在 s***e 的大作中提到】 : 不是很熟悉这种比较深的东西,不过我的理解是,非static的成员函数都事实上隐含了 : 第一个参数this,所以它的参数应该就跟应该重载的operator new不match了吧。
|
c**a 发帖数: 316 | 5 class A{
public:
void* operator new(size_t){A* p=this;};
}
编译器如是说,
d:\documents\visual studio 2005\projects\xx1\xxx1\xxx1.cpp(20
) : error C2671: 'A::operator new' : static member functions do not have
'this' pointers
【在 c**a 的大作中提到】 : ... : 都可以编译通过的。。。。
|
X****r 发帖数: 3557 | 6 你想过thrust说的了吗?
【在 c**a 的大作中提到】 : class A{ : public: : void* operator new(size_t){A* p=this;}; : } : 编译器如是说, : d:\documents\visual studio 2005\projects\xx1\xxx1\xxx1.cpp(20 : ) : error C2671: 'A::operator new' : static member functions do not have : 'this' pointers
|
c**a 发帖数: 316 | 7 。。。
编译器不是说了嘛
编译器这么说就是暗示, operator new 必须是 static的, 您老忘记加了,我给免费
加上了
你想过thrust说的了吗?
【在 X****r 的大作中提到】 : 你想过thrust说的了吗?
|
s***e 发帖数: 122 | 8 我对你对这个Warning/Error的理解有不同看法,我觉得那是因为this在new完成前是不
存在的,不过我同意你说的编译器给加上了static,嗯,我事实上同意了this在static函数里不存在:P
事实上,我用VC6, VS2008, g++ 4.3.1都测试了一下,的确operator new不管是不是
static都可以编译通过,而且连warning都没有。
我猜测可能是C++标准里面就是这么说的吧。
#include
#include
class NewTest
{
public:
NewTest() {}
virtual ~NewTest() {}
public:
void* operator new(size_t size) {
void *ptr = (void *)malloc(size);
printf("my own new\n");
return ptr;
}
void operator delete(void
【在 c**a 的大作中提到】 : 。。。 : 编译器不是说了嘛 : 编译器这么说就是暗示, operator new 必须是 static的, 您老忘记加了,我给免费 : 加上了 : : 你想过thrust说的了吗?
|
t****t 发帖数: 6806 | 9 12.5 Free store [class.free]
1 Any allocation function for a class T is a static member (even if not
explicitly declared static).
static函数里不存在:P
【在 s***e 的大作中提到】 : 我对你对这个Warning/Error的理解有不同看法,我觉得那是因为this在new完成前是不 : 存在的,不过我同意你说的编译器给加上了static,嗯,我事实上同意了this在static函数里不存在:P : 事实上,我用VC6, VS2008, g++ 4.3.1都测试了一下,的确operator new不管是不是 : static都可以编译通过,而且连warning都没有。 : 我猜测可能是C++标准里面就是这么说的吧。 : #include : #include : class NewTest : { : public:
|
s***e 发帖数: 122 | 10 cool
【在 t****t 的大作中提到】 : 12.5 Free store [class.free] : 1 Any allocation function for a class T is a static member (even if not : explicitly declared static). : : static函数里不存在:P
|
r*********r 发帖数: 3195 | |