简体   繁体   English

:: operator()是做什么的?

[英]what does ::operator() do?

    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... 结构中没有重载()运算符...

Seems pretty obvious that whoever wrote that code intended to call ::operator delete(safety) 似乎很明显,无论谁编写了旨在调用::operator delete(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). (证据: safety是一个指针;它是用::operator new(safety_size)初始化的,在他们错误地调用::operator(safety) ,将其重置为零)。

As for the purpose of the code as a whole, I have no idea -- looks like it's probably part of a pretty poor design. 至于整个代码的目的,我不知道-看起来它可能是相当糟糕的设计的一部分。

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). 肯·布鲁姆(Ken Bloom)为该代码的目的提供了一个合理的答案:保留一些在紧急情况下要释放的紧急记忆(以留出足够的呼吸空间以发出错误消息)。 See his answer for more details. 请参阅他的答案以获取更多详细信息。

A note about what this code appears to be doing: 关于此代码看起来正在做什么的注释:

On old Macs (before MacOS X, and maybe still on some low-memory handheld systems), you used to reserve some memory as a safety so that you could free it up when you ran out of memory, and so that you could use it to alert the user that something was amiss and save all their work. 在旧的Mac上(在MacOS X之前,甚至可能在某些低内存的手持式系统上),为了安全起见,您通常会保留一些内存,以便在内存不足时可以释放它,以便可以使用它提醒用户某些问题,并保存所有工作。 I saw the technique in Programming Starter Kit for Macintosh by Jim Trudeau. 我在Jim Trudeau的Macintosh编程入门工具包中看到了该技术。

So this appears to be the same kind of thing -- reserving a block of memory by size, and freeing it up when it's needed. 因此,这似乎是同一种事情-按大小保留一块内存,并在需要时释放它。 Apparently the programmer didn't want to go with the more usual idiom of safety=new char[safety_size] and delete[] safety . 显然,程序员并不想采用更常见的safety=new char[safety_size]成语: safety=new char[safety_size]delete[] safety

You're trying to call a free function operator() on a void* . 您试图在void*上调用自由函数operator() To the best of my knowledge, this does not exist. 就我所知,这是不存在的。 Hence, it does not compile for you. 因此,它不会为您编译。

I would offer alternative suggestions if I had any idea whatsoever about what you're attempting to accomplish here. 如果对您要在此处完成的工作有任何想法,我将提供其他建议。

Though obviously the ::operator delete() answer is correct, people here are still missing syntactic subtleties about the () operator. 尽管::operator delete()答案显然是正确的,但这里的人们仍然缺少关于()运算符的语法细节。

  1. This cannot be calling a method named operator , because operator is a reserved word. 这不能调用名为operator的方法,因为operator是保留字。

  2. If the code is trying to call an overloaded parentheses operator, it should say operator()(safety) -- the first () telling you it's the parentheses operator, and the second passing a parameter. 如果代码试图调用重载的括号运算符,则应该说operator()(safety) -第一个()告诉您它是括号运算符,第二个则传递参数。

  3. Even if you were to fix that, ::operator()(safety) (defined at the global scope) cannot exist, becuase (and I'll quote G++ here, because it says it better than I could) 'operator()()' must be a nonstatic member function . 即使您要解决此问题, ::operator()(safety) (在全局范围内定义)也将不存在,因为(并且我将在此处引用G ++,因为它比我可以说的更好) 'operator()()' must be a nonstatic member function

What you have is not legal C++. 您拥有的不是合法的C ++。 It calls the function call operator as a free function declared at global scope. 它将函数调用运算符作为在全局范围内声明的自由函数进行调用。 It's not legal to declare the function call operator as a free function at all at any scope. 将函数调用运算符声明为任何范围内的自由函数都是非法的。 It must be declared as a non-static member function. 必须将其声明为非静态成员函数。

If it were legal, it would be calling a function that looked like this: 如果合法,它将调用如下所示的函数:

void operator ()(void *foo)
{
   ::std::cout << "operator()(void *)\n";
}

But if you put such code into a compiler the compiler will tell you it's not legal. 但是,如果将此类代码放入编译器,编译器会告诉您这是不合法的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM