Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Is it possible to bind the "this" pointer

Is it possible to bind the "this" pointer

Scheduled Pinned Locked Moved C / C++ / MFC
c++question
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    elelont2
    wrote on last edited by
    #1

    Hi, Is it possible to bind the "this" pointer to make a callback into a non-static class member function (c++98, no boost). I don't think the "this" pointer is passed directly as a parameter to the function?

    typedef void (*CallBackType)(void);

    ...

    obj->SetCallback(std::bind1st(&MyClass::CallBackFunc, this));

    C C 2 Replies Last reply
    0
    • E elelont2

      Hi, Is it possible to bind the "this" pointer to make a callback into a non-static class member function (c++98, no boost). I don't think the "this" pointer is passed directly as a parameter to the function?

      typedef void (*CallBackType)(void);

      ...

      obj->SetCallback(std::bind1st(&MyClass::CallBackFunc, this));

      C Offline
      C Offline
      CPallini
      wrote on last edited by
      #2

      No, it is not possible (at least as far as I know). From bind1st at C++ Reference:

      Return function object with first parameter bound This function constructs an unary function object from the binary function object op by binding its first parameter to the fixed value x.

      A method taking one argument is not exactly a binary function.

      THESE PEOPLE REALLY BOTHER ME!! How can they know what you should do without knowing what you want done?!?! -- C++ FQA Lite

      1 Reply Last reply
      0
      • E elelont2

        Hi, Is it possible to bind the "this" pointer to make a callback into a non-static class member function (c++98, no boost). I don't think the "this" pointer is passed directly as a parameter to the function?

        typedef void (*CallBackType)(void);

        ...

        obj->SetCallback(std::bind1st(&MyClass::CallBackFunc, this));

        C Offline
        C Offline
        CubbiMew
        wrote on last edited by
        #3

        You could use bind1st if CallBackFunc took one parameter:

        void CallBackFunc(int) { ... }
        ...
        obj->SetCallback(std::bind1st(std::mem_fun(&MyClass::CallBackFunc), this));

        Since it takes nothing, you may have to write your own function template that takes a callable and a pointer that returns a small function object that stores the callable and the pointer as members and invokes them with operator(): as always, by refusing to use boost you're forcing yourself to reimplement parts of it.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups