member functions
-
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? -
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?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. Soint TheNotifyFunc(int, int, int, int)
is actuallyint TheNotifyFunc(class2* this, int, int, int, int);
, although the language hides that detail. Then, how do you intend to pass thethis
parameter fromclass1
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 yourChild
class to be t -
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. Soint TheNotifyFunc(int, int, int, int)
is actuallyint TheNotifyFunc(class2* this, int, int, int, int);
, although the language hides that detail. Then, how do you intend to pass thethis
parameter fromclass1
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 yourChild
class to be ti read the articles, but i dont think they apply to me. my goal is to pass the member function pointer to a win32 WNDPROC.
-
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.
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/[^]