私人操作员删除 [英] private operator delete

查看:153
本文介绍了私人操作员删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

公共运营商新,私人运营商删除:获取C2248无法访问私有成员使用新


http://efesx.com/2009/12/01/public-operator-new-and- private-operator-delete /



在本文中,我读到此代码应该给出一个错误:

  #include< cstdlib> 

struct Try {
Try(){/ * o / * /}

void * operator new(size_t size){
return malloc尺寸);
}

private:
void operator delete(void * obj){
free(obj);
}
};

int main(){
Try * t = new Try();

return 0;
}

我用gcc 4.7.1尝试:

 编译完成时出错:source.cpp:在函数'int 
main()':source.cpp:11:14:error: static void Try :: operator
delete(void *)'is private source.cpp:17:22:error:在这个上下文中
source.cpp:11:14:error: :operator delete(void *)'is
private source.cpp:17:22:error:在此上下文中source.cpp:17:10:
警告:未使用的变量't'[-Wunused-变量]



在本文中的评论中,我看到了此链接 - 公共运算符new,私人运算符delete:获取C2248无法访问私有成员 ;当使用新的



如果我确认它是正确的,它不会编译,因为编译器应该避免任何内存泄漏的情况下,从构造函数抛出的异常调用适当的操作符删除。但为什么这个代码会编译和工作?

  #include< cstdlib> 

struct Try {
void * operator new(size_t size){
return malloc(size);
}

private:
void operator delete(void * obj){
free(obj);
}
};

int main(){
Try * t = new Try;

return 0;
}

标准是否正确?



这个代码呢?

  #include< cstdlib> 

struct Try {
void * operator new(size_t size){
return malloc(size);
}

private:
void operator delete(void * obj){
free(obj);
}
};

int main(){
Try * t = new Try();

return 0;
}

它不能用gcc 4.7.1编译。

$



Comeau不能编译所有这些例子:

 ComeauTest.c,第15行:error:functionTry :: operator delete
(在第9行声明)无法访问Try * t = new Try; ^

任何人都可以详细解释一下我吗?

解决方案


  • 似乎是第一个例子。



第二个和第三个例子处理POD类型。然后初始化差异

在第二个示例中,您的结构保持未初始化。

/ li>


编辑



c $ c> operator new 本身可以抛出异常。标准(c ++ 11 darft说):


如果 new 通过抛出异常,它可以通过调用释放函数(3.7.4.2)释放存储
。如果
分配的类型是非数组类型,则分配函数的名称为
operator new ,并且释放函数的名称为 operator delete


这有点不清楚, / em>释放存储。



无论如何,你可以尝试使用not-throwing new version:

  void * operator new(size_t size,std :: nothrow_t)throw 
return malloc(size);
}


Possible Duplicate:
Public operator new, private operator delete: getting C2248 “can not access private member” when using new

http://efesx.com/2009/12/01/public-operator-new-and-private-operator-delete/

In this article i read that this code should give an error:

#include <cstdlib>

struct Try {
        Try () { /* o/ */ }

        void *operator new (size_t size) {
            return malloc(size);
        }

    private:
        void operator delete (void *obj) {
            free(obj);
        }
};

int main () {
    Try *t = new Try();

    return 0;
}

I tried it with gcc 4.7.1:

Compilation finished with errors: source.cpp: In function 'int
main()': source.cpp:11:14: error: 'static void Try::operator
delete(void*)' is private source.cpp:17:22: error: within this context
source.cpp:11:14: error: 'static void Try::operator delete(void*)' is
private source.cpp:17:22: error: within this context source.cpp:17:10:
warning: unused variable 't' [-Wunused-variable]

In comment in this article i saw this link - Public operator new, private operator delete: getting C2248 "can not access private member" when using new

If i unserstand it correct, it doesn't compile because compiler should avoid any memory leaks in situations when there are exception thrown from constructor by calling the appropriate operator delete. But why this code compile and works?

#include <cstdlib>

struct Try {
        void *operator new (size_t size) {
            return malloc(size);
        }

    private:
        void operator delete (void *obj) {
            free(obj);
        }
};

int main () {
    Try *t = new Try;

    return 0;
}

Is it correct by standard or not?

And what about this code?

#include <cstdlib>

struct Try {
        void *operator new (size_t size) {
            return malloc(size);
        }

    private:
        void operator delete (void *obj) {
            free(obj);
        }
};

int main () {
    Try *t = new Try();

    return 0;
}

It doesn't compile with gcc 4.7.1.

And how things like this should be implemented in the standard library?

Comeau doesn't compile all these examples:

"ComeauTest.c", line 15: error: function "Try::operator delete"
(declared at line 9) is inaccessible Try *t = new Try; ^

Can anyone explain me this in detail, please?

解决方案

  • Seems, you are right about the first example.

Second and third examples deal with POD type. And there initialization differences play role.

  • In the second example your structure left uninitialized. No problem appear.

  • In opposite, in third example structure does initialize, so you get first case.

Edit:

Then, operator new itself can throw an exception. Standard (c++11 darft says):

If the new expression terminates by throwing an exception, it may release storage by calling a deallocation function (3.7.4.2). If the allocated type is a non-array type, the allocation function’s name is operator new and the deallocation function’s name is operator delete.

It's bit unclear, what authors wanted to express saying it may release storage. It seems to be implementation defined, if it is released.

Anyway, you can try using not-throwing new version:

void *operator new (size_t size, std::nothrow_t) throw() {
    return malloc(size);
}

这篇关于私人操作员删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆