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 (sa开发者_运维技巧fety) {
::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)
(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.
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
.
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.
Though obviously the ::operator delete()
answer is correct, people here are still missing syntactic subtleties about the ()
operator.
This cannot be calling a method named
operator
, becauseoperator
is a reserved word.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.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
.
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.
精彩评论