:: operator()是做什么的? [英] what does ::operator() do?

查看:165
本文介绍了:: operator()是做什么的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

    struct reserved_memory
    {
     void *safety;
     size_t safety_size;
     reserved_memory(size_t size) : safety_size(size)
     {
       init();
     }

     bool use() {
        if (safety) {
            ::operator(safety); 
            safety=0;
            return true;
        } else
            return false;
     }
    private:
     void init() 
     {
        safety=::operator new(safety_size);
     }
   }

我有未编译的代码-而且我也从未见过.这是在调用构造函数吗?结构中没有重载()运算符...

I have this code that isn't compiling - and I also have never seen this before. Is this calling the constructor? There is no overloaded () operator in the struct...

推荐答案

似乎很明显,无论是谁编写的该代码旨在调用::operator delete(safety)

Seems pretty obvious that whoever wrote that code intended to call ::operator delete(safety)

(证据:safety是一个指针;它是用::operator new(safety_size)初始化的,在他们错误地调用::operator(safety)之后,将其重置为零).

(evidence: safety is a pointer; it was initialised with ::operator new(safety_size), and after they erroneously call ::operator(safety) they reset it to zero).

至于整个代码的目的,我不知道-看起来它可能是相当糟糕的设计的一部分.

肯布鲁姆(Ken Bloom)为该代码的目的提供了一个合理的答案:保留一些在紧急情况下要释放的紧急记忆(以留出足够的呼吸空间以发出错误消息).有关更多详细信息,请参见他的回答.

Ken Bloom has provided a plausible answer for the purpose of the code: reserving some emergency memory to be released in dire circumstances (to give enough breathing room to be able to emit an error message). See his answer for more details.

这篇关于:: operator()是做什么的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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