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. Pointer to member function

Pointer to member function

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
2 Posts 2 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.
  • A Offline
    A Offline
    AndreFratelli
    wrote on last edited by
    #1

    Hi guys =) need a little help here... Shouldn't this work?

    class MyClass
    {
    public:
    void method() {}
    void (MyClass::*_callback)();
    };

    int main()
    {
    MyClass b;

    b._callback = &MyClass::method;

    (b.*_callback)(); // error C2065: '_callback' : undeclared identifier

    return 0;
    }

    Fratelli

    S 1 Reply Last reply
    0
    • A AndreFratelli

      Hi guys =) need a little help here... Shouldn't this work?

      class MyClass
      {
      public:
      void method() {}
      void (MyClass::*_callback)();
      };

      int main()
      {
      MyClass b;

      b._callback = &MyClass::method;

      (b.*_callback)(); // error C2065: '_callback' : undeclared identifier

      return 0;
      }

      Fratelli

      S Offline
      S Offline
      Stephen Hewitt
      wrote on last edited by
      #2

      No, it shouldn’t work. Replace this:

      (b.*_callback)();

      with this:

      (b.*(b._callback))();

      C++ member function pointers are not like delegates in C#. The pointer MyClass::_callback identifies a member function of MyClass which returns void and takes no parameters (and has the calling convention __thiscall): it doesn’t specify which instance of MyClass. The fact that the pointer is a member of the class is incidental and is probably what’s confusing you.

      Steve

      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