Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

7
  • 7
    This indeed is the standard solution for C-style windowing libraries - each window provided by the framework can keep some arbitrary cookie - which is most frequently used to hold the actual instance (object) pointer, and it is used in a static method to bounce through to the actual object. And I would call this no-extra-cost as the framework is reserving these 32- (or 64-) bits anyway so no storage cost and you'd have to do something to get back to your actual object anyway. Commented Dec 19, 2020 at 16:43
  • And while it varies from library to library, some libraries like Python do make it zero-cost. They make the getPointer function into a macro, rather than a real function, removing all of the extra cost. Commented Dec 19, 2020 at 18:16
  • 4
    Note that you could avoid the static function by using a lambda as OP tried. glfwSetWindowCloseCallback(mHandle, [](auto window) { static_cast<Window*>(glfwGetWindowUserPointer(wHandle))->onClose(); }) for example. Commented Dec 19, 2020 at 19:18
  • @Christoph cool. Lambdas didn’t exist in C++ last time I used it. Commented Dec 19, 2020 at 21:41
  • 1
    Also, in onCloseWrapper it should be Window* w = static_cast<Window*>(glfwGetWindowUserPointer(wHandle)); w->onClose(); Commented Dec 20, 2020 at 12:29