Click here to Skip to main content
16,004,425 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
Questionneed a multimedia library!? Pin
ThinkingPrometheus26-Jun-05 10:34
ThinkingPrometheus26-Jun-05 10:34 
AnswerRe: need a multimedia library!? Pin
Christian Graus26-Jun-05 16:03
protectorChristian Graus26-Jun-05 16:03 
Questionhow to get control of flash formular? Pin
Anonymous26-Jun-05 10:23
Anonymous26-Jun-05 10:23 
GeneralHELP: cpu load graph plotting program Pin
kevingpo26-Jun-05 7:52
kevingpo26-Jun-05 7:52 
GeneralRe: HELP: cpu load graph plotting program Pin
Ravi Bhavnani26-Jun-05 8:45
professionalRavi Bhavnani26-Jun-05 8:45 
GeneralRe: HELP: cpu load graph plotting program Pin
Anonymous26-Jun-05 10:19
Anonymous26-Jun-05 10:19 
Generalmember functions Pin
zildjohn0126-Jun-05 7:39
zildjohn0126-Jun-05 7:39 
GeneralRe: member functions Pin
Jose Lamas Rios26-Jun-05 16:36
Jose Lamas Rios26-Jun-05 16:36 
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 tied (coupled) to a particular Parent class? That is, the only thing your Child class needs to know about (and depend on) from its "parent", is a way to send it some specific notification.

If that is the case, I humbly suggest you read this article[^], and then program something like this:

/////////////////////////////////////////////
// IChildEventTarget.h
 
#pragma once
 
#include "cppinterfaces.h"
 
DeclareInterface(IChildEventTarget)
	virtual int OnChildEvent(int n1, int n2, int n3, int n4) = 0;
EndInterface(IChildEventTarget)
 
/////////////////////////////////////////////
// child.h
 
#pragma once
 
interface IChildEventTarget;
 
class Child
{
private:
	IChildEventTarget* m_pEventTarget;
 
public:
	Child(IChildEventTarget* pEventTarget)
 
	void SomeChildFunction();
};
 
 
/////////////////////////////////////////////
// child.cpp
 
#include "child.h"
 
// Note Child only depends on IChildEventTarget
// rather than the entire Parent class (which in
// turn, would make it depend on all Parent's
// dependencies too)
#include "IChildEventTarget.h"
 
Child::Child(IChildEventTarget* pEventTarget)
: m_pEventTarget(pEventTarget)
{
}
 
void Child::SomeChildFunction()
{
	// Do some stuff here
 
	// [...]
 
	// and notify the event target
	if (m_pEventTarget)
		m_pEventTarget->OnChildEvent(n1, n2, n3, n4);
}
 
 
/////////////////////////////////////////////
// parent.h
 
#pragma once
 
#include "IChildEventTarget.h"
 
class Child;
 
class Parent : implements IChildEventTarget.h
{
private:
	Child* m_pChild;
 
public:
	Parent();
	~Parent();
 
	int OnChildEvent(int n1, int n2, int n3, int n4);
};
 
/////////////////////////////////////////////
// parent.cpp
 
/* same as before */


This is a much cleaner design, because your Child does not depend anymore in any specific "parent" implementation, which is what I think you was looking for with the generic function pointer. The child class does not need to be modified (not even recompiled) because of irrelevant changes in the parent class, and any other unrelated class can act as a parent as long as it implements the IChildEventTarget interface.

I've found this interface approach very useful in many cases, not just for events and event targets. However, for this specific case, you should also take a look at this excellent article[^] by Don Clugston[^].

Hope that helps,

--
jlr
http://jlamas.blogspot.com/[^]
GeneralRe: member functions Pin
zildjohn0129-Jun-05 9:23
zildjohn0129-Jun-05 9:23 
GeneralRe: member functions Pin
Jose Lamas Rios29-Jun-05 11:03
Jose Lamas Rios29-Jun-05 11:03 
QuestionHow to develop real-time communication program through RTP Pin
AntonlioX26-Jun-05 4:01
AntonlioX26-Jun-05 4:01 
Generalabout Gina in XP Pin
Veelone26-Jun-05 1:55
Veelone26-Jun-05 1:55 
GeneralRe: about Gina in XP Pin
LunaticFringe26-Jun-05 3:55
LunaticFringe26-Jun-05 3:55 
General Looking for Developers to try new C++ XML interface. Pin
Stuart Konen25-Jun-05 20:09
Stuart Konen25-Jun-05 20:09 
GeneralRe: Looking for Developers to try new C++ XML interface. Pin
Trollslayer25-Jun-05 22:14
mentorTrollslayer25-Jun-05 22:14 
GeneralRe: Looking for Developers to try new C++ XML interface. Pin
Ravi Bhavnani26-Jun-05 3:06
professionalRavi Bhavnani26-Jun-05 3:06 
GeneralRe: Looking for Developers to try new C++ XML interface. Pin
Stuart Konen26-Jun-05 7:52
Stuart Konen26-Jun-05 7:52 
GeneralRe: Looking for Developers to try new C++ XML interface. Pin
ThatsAlok26-Jun-05 19:00
ThatsAlok26-Jun-05 19:00 
GeneralQuestion on IMPLEMENT_DYNCREATE In Multiple Inheritance Case Pin
yccheok25-Jun-05 19:44
yccheok25-Jun-05 19:44 
GeneralRe: Question on IMPLEMENT_DYNCREATE In Multiple Inheritance Case Pin
John M. Drescher26-Jun-05 4:16
John M. Drescher26-Jun-05 4:16 
GeneralRe: Question on IMPLEMENT_DYNCREATE In Multiple Inheritance Case Pin
Jose Lamas Rios26-Jun-05 16:54
Jose Lamas Rios26-Jun-05 16:54 
GeneralWindows Services Pin
Laji5925-Jun-05 16:52
Laji5925-Jun-05 16:52 
GeneralRe: Windows Services Pin
Jose Lamas Rios25-Jun-05 18:26
Jose Lamas Rios25-Jun-05 18:26 
GeneralRe: Windows Services Pin
Laji5925-Jun-05 19:04
Laji5925-Jun-05 19:04 
GeneralRe: Windows Services Pin
fakefur25-Jun-05 19:24
fakefur25-Jun-05 19:24 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.