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

member functions

Scheduled Pinned Locked Moved C / C++ / MFC
c++regexhelptutorialquestion
4 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.
  • Z Offline
    Z Offline
    zildjohn01
    wrote on last edited by
    #1

    hi, i am aware how to take the address of a function, but what about a bound member function? The compiler won't let me do this. here is my code, i try to pass a function pointer to a child class to notify the parent of events: class1.h class class1 { public: class1(int (*)(int, int, int, int)); }; class1.cpp #include "class1.h" class1::class1(int (*notifyFunc)(int, int, int, int)) {} class2.h #include "class1.h" class class2 { private: class1 *client; int TheNotifyFunc(int, int, int, int); public: class2(); }; class2.cpp #include "class2.h" class2::class2() { client = new class1(this->TheNotifyFunc); // cannot take addr of bound member function } int class2::TheNotifyFunc(int, int, int, int) { // do stuff here return 0; } main.cpp #include "class2.h" void main() { new class2(); } my compiler error is: Compiling... class2.cpp class2.cpp(4) : error C2664: 'class1::class1(int (__cdecl *)(int,int,int,int))' : cannot convert parameter 1 from 'int (int,int,int,int)' to 'int (__cdecl *)(int,int,int,int)' None of the functions with this name in scope match the target type any workarounds?

    J 1 Reply Last reply
    0
    • Z zildjohn01

      hi, i am aware how to take the address of a function, but what about a bound member function? The compiler won't let me do this. here is my code, i try to pass a function pointer to a child class to notify the parent of events: class1.h class class1 { public: class1(int (*)(int, int, int, int)); }; class1.cpp #include "class1.h" class1::class1(int (*notifyFunc)(int, int, int, int)) {} class2.h #include "class1.h" class class2 { private: class1 *client; int TheNotifyFunc(int, int, int, int); public: class2(); }; class2.cpp #include "class2.h" class2::class2() { client = new class1(this->TheNotifyFunc); // cannot take addr of bound member function } int class2::TheNotifyFunc(int, int, int, int) { // do stuff here return 0; } main.cpp #include "class2.h" void main() { new class2(); } my compiler error is: Compiling... class2.cpp class2.cpp(4) : error C2664: 'class1::class1(int (__cdecl *)(int,int,int,int))' : cannot convert parameter 1 from 'int (int,int,int,int)' to 'int (__cdecl *)(int,int,int,int)' None of the functions with this name in scope match the target type any workarounds?

      J Offline
      J Offline
      Jose Lamas Rios
      wrote on last edited by
      #2

      I don't think the way you are trying to do it will work anyway... Why do you want to pass "a bound member function" in the first place? Presumably because you want/need that function to access some data of a particular instance of class2, isn't it? Non static class member functions include an implicit first parameter named this, which is a pointer to the particular instance for which the method is invoked. So int TheNotifyFunc(int, int, int, int) is actually int TheNotifyFunc(class2* this, int, int, int, int);, although the language hides that detail. Then, how do you intend to pass the this parameter from class1 when you need to invoke the function? If you don't actually need to access instance data in TheNotifyFunc(), then you should declare it as static. If you do need to access instance data, you could, at first, program something like this:

      /////////////////////////////////////////////
      // child.h
       
      #pragma once
       
      class Parent;
       
      class Child
      {
      private:
      Parent* m_pParent;
       
      public:
      Child(Parent* pParent)
       
      void SomeChildFunction();
      };
       
       
      /////////////////////////////////////////////
      // child.cpp
       
      #include "child.h"
      #include "parent.h"
       
      Child::Child(Parent* pParent)
      : m_pParent(pParent)
      {
      }
       
      void Child::SomeChildFunction()
      {
      // Do some stuff here
       
      // [...]
       
      // and notify parent
      if (m_pParent)
      m_pParent->OnChildEvent(n1, n2, n3, n4);
      }
       
       
      /////////////////////////////////////////////
      // parent.h
       
      #pragma once
       
      class Child;
       
      class Parent
      {
      private:
      Child* m_pChild;
       
      public:
      Parent();
      ~Parent();
       
      int OnChildEvent(int n1, int n2, int n3, int n4);
      };
       
       
      /////////////////////////////////////////////
      // parent.cpp
       
      #include "parent.h"
      #include "child.h"
       
      Parent::Parent()
      {
      m_pChild = new Child(this);
      }
       
      Parent::~Parent()
      {
      delete m_pChild;
      }
       
      int Parent::OnChildEvent(int n1, int n2, int n3, int n4)
      {
      // do stuff here
      }

      However, the fact that you were trying to use generic notifier functions rather than plain old object pointers and method calls, makes me think this approach might not be appropriate in your case. Indeed, what if you need your Child objects to have parents of different non related classes? In other words, what if you don't want your Child class to be t

      Z 1 Reply Last reply
      0
      • J Jose Lamas Rios

        I don't think the way you are trying to do it will work anyway... Why do you want to pass "a bound member function" in the first place? Presumably because you want/need that function to access some data of a particular instance of class2, isn't it? Non static class member functions include an implicit first parameter named this, which is a pointer to the particular instance for which the method is invoked. So int TheNotifyFunc(int, int, int, int) is actually int TheNotifyFunc(class2* this, int, int, int, int);, although the language hides that detail. Then, how do you intend to pass the this parameter from class1 when you need to invoke the function? If you don't actually need to access instance data in TheNotifyFunc(), then you should declare it as static. If you do need to access instance data, you could, at first, program something like this:

        /////////////////////////////////////////////
        // child.h
         
        #pragma once
         
        class Parent;
         
        class Child
        {
        private:
        Parent* m_pParent;
         
        public:
        Child(Parent* pParent)
         
        void SomeChildFunction();
        };
         
         
        /////////////////////////////////////////////
        // child.cpp
         
        #include "child.h"
        #include "parent.h"
         
        Child::Child(Parent* pParent)
        : m_pParent(pParent)
        {
        }
         
        void Child::SomeChildFunction()
        {
        // Do some stuff here
         
        // [...]
         
        // and notify parent
        if (m_pParent)
        m_pParent->OnChildEvent(n1, n2, n3, n4);
        }
         
         
        /////////////////////////////////////////////
        // parent.h
         
        #pragma once
         
        class Child;
         
        class Parent
        {
        private:
        Child* m_pChild;
         
        public:
        Parent();
        ~Parent();
         
        int OnChildEvent(int n1, int n2, int n3, int n4);
        };
         
         
        /////////////////////////////////////////////
        // parent.cpp
         
        #include "parent.h"
        #include "child.h"
         
        Parent::Parent()
        {
        m_pChild = new Child(this);
        }
         
        Parent::~Parent()
        {
        delete m_pChild;
        }
         
        int Parent::OnChildEvent(int n1, int n2, int n3, int n4)
        {
        // do stuff here
        }

        However, the fact that you were trying to use generic notifier functions rather than plain old object pointers and method calls, makes me think this approach might not be appropriate in your case. Indeed, what if you need your Child objects to have parents of different non related classes? In other words, what if you don't want your Child class to be t

        Z Offline
        Z Offline
        zildjohn01
        wrote on last edited by
        #3

        i read the articles, but i dont think they apply to me. my goal is to pass the member function pointer to a win32 WNDPROC.

        J 1 Reply Last reply
        0
        • Z zildjohn01

          i read the articles, but i dont think they apply to me. my goal is to pass the member function pointer to a win32 WNDPROC.

          J Offline
          J Offline
          Jose Lamas Rios
          wrote on last edited by
          #4

          The usual approach in these cases is to pass a global function or a static member function, eventually registering the callback along with a user data value, that gets passed back as a parameter in each callback. As the user data value, you may pass a pointer to some object, and the static class member can use it to access other non static members of the class. -- jlr http://jlamas.blogspot.com/[^]

          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