what does ::operator() do?

允我心安 提交于 2019-12-08 08:37:18

问题


    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...


回答1:


Seems pretty obvious that whoever wrote that code intended to call ::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).

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). See his answer for more details.




回答2:


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. I saw the technique in Programming Starter Kit for Macintosh by Jim Trudeau.

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.




回答3:


You're trying to call a free function operator() on a void*. 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.




回答4:


Though obviously the ::operator delete() answer is correct, people here are still missing syntactic subtleties about the () operator.

  1. This cannot be calling a method named operator, because operator is a reserved word.

  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.

  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.




回答5:


What you have is not legal 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.



来源:https://stackoverflow.com/questions/4687166/what-does-operator-do

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!