成员函数指针——只有一个地址?

发布于 2024-11-02 01:42:25 字数 251 浏览 0 评论 0原文

http://www.codeproject.com/KB/cpp/fastdelegate2.aspx

在上面文章的介绍的第二段中,它说:“这是由于存储成员函数和对其进行成员函数调用的绑定对象需要昂贵的堆内存分配。” ..我不明白这个?它实际上必须复制并存储对象和成员函数吗?它不是只存储成员函数的地址吗?

http://www.codeproject.com/KB/cpp/fastdelegate2.aspx

In the second paragraf of the introduction in the above article it says: "This is due to the expensive heap memory allocation that is required to store the member function and the bound object on which member function call is made." .. I dont get this? Does it actualy have to copy and store the object and the member function? Doesn't it only store the address of the member function?

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

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

发布评论

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

评论(2

尘世孤行 2024-11-09 01:42:25

Boost.Function 比原始函数指针更通用、更强大:它们可以存储任何可使用特定签名调用的内容。然而,这种灵活性会带来存储和运行时间方面的成本。

Boost.Function 的杂项注释部分文档对此进行了更多讨论,但总结一下:

  • Boost.Function 对象在内部存储一个成员函数指针和两个数据指针。
  • 如果存储大于特定大小的函子,则可能需要堆分配。
  • 调用 Boost.Function 对象会导致通过函数指针进行一次或两次调用,具体取决于具体存储的内容。

话虽如此,我已经广泛使用了 Boost.Function,并且从未遇到过在分析时实际显示其存储或运行时成本的情况,因此这是否重要将取决于您的实际使用情况。

Boost.Function is more general and powerful than raw function pointers: they can store anything that is callable with a particular signature. However, there is a cost in storage and run-time associated with that flexibility.

The Miscellaneous Notes section of the Boost.Function documentation talks a bit more about this, but to summarize:

  • A Boost.Function object stores a member function pointer and two data pointers internally.
  • It may require a heap allocation if storing a functor that is larger than a certain size.
  • Calling a Boost.Function object results in either one or two calls through a function pointer, depending on what exactly was stored.

Having said all that, I've used Boost.Function extensively and never had a situation where its storage or run-time costs actually showed up when profiling, so whether any of this is important or not will depend on your actual usage.

猛虎独行 2024-11-09 01:42:25

,您不能仅使用指向方法的指针来调用成员函数。原因是因为方法是在上下文 (this)(调用该方法的对象)内调用的。如果您只有成员函数指针,您将无法知道该方法应该应用于哪个对象。但是,如果成员函数是静态,则它具有上下文,因为可以在不实例化对象的情况下调用静态成员函数。

因此,要调用成员函数,您需要一个指向该函数的指针,加上一些对定义将在其中调用成员函数的上下文的对象的引用。

这能回答你的问题吗?

No, you can't call a member function with only the pointer to the method. The reason is because methods are called within a context (this), the object on which the method is called. If you only have the member function pointer you can't know to which object the method should be applied. However, if the member function is static, then it does NOT have a context, because static member functions can be called without instantiating an object.

So to call a member function you need a pointer to the function, PLUS some reference to an object defining the context in which the call to member function will take place.

Does this answer your question?

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