Hello
I need help setting up two classes to work 'cooperatively'!
I hope to state it as simply as possible.
Two classes:
- Child, many instances exist
- Parent, only one instance exists, manages all Child instances
- Child class exists as a library on this site, it works well, and I wish not to change it
- Child class is setup with a function pointer to callback a 'general function'
- I wish the child to callback functions in parent instead
- It would be nice if no hard-coding or explicit dependency on 'ParentClass' were required;
in other words, I may wish to use the Child class with a different FatherClass or MotherClass;
I hope not to have to go back to modify Child class for this;
What follows are snippets/simplification of Child/Parent classes as I imagine them.
I have embedded comments where I need help.
Any and all comments are very well appreciated.
BTW: I am an experienced VB programmer, and so not a total newbie.
However, I am completely foreign to C++, and have only recently been exposed to pointers and references.
Any links to relevant material to help me understand will also be appreciated.
Ron!
<Child.h>
//A Child will have sibling instances, children, created by Parent;
//Under specific conditions, a child method (eg. Child.Enter, Child.Exit, ...) will be invoked;
// This Child then to calls back a corresponding unique Parent.Method;
Class Child()
Public
Child::Child(); //Child constructor;
// I am not sure if the following 2 pointers are required to accomplish what I want;
// I need help with the following construct;
Child::Setup(void *parentObject, void *parentFunction());
void fn();
Private
void *ptToParent;
void *ptToParentFn();
<Child.cpp>
Child::Child() {}; //Child constructor;
Child::Setup(void *parentObject, void *parentFunction());
// I need help with the following two constructs;
ptToParent = parentObject
ptToParentFn = parentFunction;
}
//child method whose job it is to call back parent method;
Child::Enter() {
//I need help here as well;
??? ptToParent->ptToParentFn() ??? // the crux of this post!
}
Child::Exit() {
}
<Parent.h>
//The parent knows all children, and gives each child unique call back numbers;
//Parent spawns Children: Child1, Child2, Childn;
//Parents sets up each Child with its own distinct callback function
Class Parent {
Public:
Parent:: Parent() {Child1(), Child2(), Child3() };
Parent:: SetupChildren();
Child Child1;
Child Child2;
Child Child3;
void fn1()
void fn2()
void fn3()
Private:
}
<Parent.cpp>
Parent::SetupChildren() {
Child1(*this, &fn1);
Child2(*this, &fn2);
Child3(*this, &fn3);
}