::operator() 做什么?

发布于 2024-10-12 09:24:17 字数 510 浏览 4 评论 0原文

    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);
     }
   }

我有这段代码无法编译 - 而且我以前也从未见过这个。这是调用构造函数吗?结构体中没有重载 () 运算符...

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

想你的星星会说话 2024-10-19 09:24:17

似乎很明显,编写该代码的人打算调用 ::operator delete(safety)

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

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

Ken Bloom 为代码的目的提供了一个看似合理的答案:保留在紧急情况下释放一些紧急内存(以提供足够的喘息空间以便能够发出错误消息)。有关更多详细信息,请参阅他的回答

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.

最后的乘客 2024-10-19 09:24:17

关于这段代码似乎在做什么的注释:

在旧的 Mac 上(在 MacOS X 之前,也许仍在一些低内存手持系统上),您过去常常保留一些内存作为安全措施,以便在运行时可以释放它内存不足,以便您可以使用它来提醒用户出现问题并保存他们的所有工作。我在 Jim Trudeau 的《Macintosh 编程入门套件》中看到了该技术。

所以这看起来是同一类事情——按大小保留一块内存,并在需要时释放它。显然,程序员不想使用更常见的习惯用法:safety=new char[safety_size]delete[] safety

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.

简美 2024-10-19 09:24:17

您正尝试在 void* 上调用自由函数 operator()。据我所知,这并不存在。因此,它不会为您编译。

如果我对您想要在这里完成的任务有任何想法,我会提供替代建议。

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.

2024-10-19 09:24:17

虽然显然 ::operator delete() 答案是正确的,但这里的人们仍然缺少关于 () 运算符的语法微妙之处。

  1. 这不能调用名为 operator 的方法,因为 operator 是保留字。

  2. 如果代码尝试调用重载的括号运算符,则应显示 operator()(safety) ——第一个 () 告诉您这是括号运算符,第二个传递参数。

  3. 即使你要解决这个问题,::operator()(safety)(在全局范围内定义)也不可能存在,因为(我会在这里引用 G++,因为它说它比我更好)'operator()()'必须是一个非静态成员函数

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.

芯好空 2024-10-19 09:24:17

你所拥有的不是合法的C++。它将函数调用运算符作为在全局范围内声明的自由函数来调用。在任何范围内将函数调用运算符声明为自由函数都是不合法的。它必须声明为非静态成员函数。

如果它是合法的,它将调用一个如下所示的函数:

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

但如果你将这样的代码放入编译器中,编译器会告诉你它不合法。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文