Encapsulating an existing class


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.


//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()
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();

void *ptToParent;
void *ptToParentFn();

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() {

//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 {
Parent:: Parent() {Child1(), Child2(), Child3() };
Parent:: SetupChildren();

Child Child1;
Child Child2;
Child Child3;

void fn1()
void fn2()
void fn3()


Parent::SetupChildren() {
Child1(*this, &fn1);
Child2(*this, &fn2);
Child3(*this, &fn3);

Parent myParentObject; myParentObject.Child1.Enter(); //will cause fn1() to be invoked myParentObject.Child2.Enter(); //will cause fn2() to be invoked
  • Child class exists as a library on this site, it works well, and I wish not to change it

Without changing the child class, there will be no way for it to be aware of a new parent, since it currently (apparently) does not need/have a parent.

Perhaps the key to understanding what you want to do is for you to be a bit less vague. What existing library is this child class?

Hello PaulS,

Appologies for the vagueness, in my attempt to generalize the question and related concepts..

The child class is FiniteStateMachine found here http://www.arduino.cc/playground/Code/FiniteStateMachine.

The author was kind in answering my questions; he actually pointed out that the 'State' object in this class calls back to a general function.

As far as a very general description on the use of the FiniteStateMachine goes, it governs the sensing of ONE PushButton switch. I've written a sketch using the FSM class as it stands, and I'm rather thrilled at how well it works !! :) The FSM reports back things like Click, DoubleClick, ClickAndHold, DoubleClickAndHold, and ButtonUp. It's fast, and very few lines of code are required, thanks to the finite state machine concept. So, one FSM, governs the many states (3, declared in a sketch) of one button. The encapsulating class I am working on is to tie together the 3 states, FSM, and callback functions into a neat little package. Multiple instances of the resulting Parent class will be convenient to instantiate multiple buttons working in a similar way. These buttons will eventually govern a motorized astronomical telescope driven by stepper motors.

Question on a comment in your post: Assuming one were to make the child (State) aware of a parent (the class I am now working on), could one make this a generic awareness? In other words, if I write a FAtherParent, MotherParent, or even a GrandParent class in the future, could the State class be used arbitrarily with any parent?

Thanks in advance!