Function pointers between classes

I have 2 classes: MainClass and SubClass

SubClass contains a lot of variables and methods to alter the variables. When I alter a variable in SubClass I want it to call a function in MainClass to tell it the new value of the variable. This is so MainClass can do things with that new value.

The code I have at the moment is as follows:

MainClass.h

#include SubClass.h
class MainClass
{
public:
    MainClass();
    ~MainClass();
    void SubValueChanged(byte new_value);
private:
    SubClass* sub_;
}

MainClass.cpp

#include MainClass.h
MainClass::MainClass()
{
    sub_= new SubClass(SubValueChanged);
} 
MainClass::~MainClass()
{
}
MainClass::SubValueChanged(byte new_value)
{
// do something here with new_value
}

SubClass.h

typedef void (ByteFuncPtr*)(byte);
class SubClass
{
public:
    SubClass(void (*val_func)(byte));
       ~SubClass();
        ChangeValue(byte new_val);
private:
        static ByteFuncPtr val_ptr_;
        byte value;
}

SubClass.cpp

#include SubClass.h
SubClass::SubClass(void (*val_func)(byte))
{
    val_ptr_ = val_func;

}
SubClass::~SubClass()
{
}
SubClass:ChangeValue(byte new_val)
{
     value = new_val;
     val_ptr_(new_val);
}

Now I'm aware what I'm doing wrong - When I declare sub_ in MainClass::MainClass(), I need to send MainClass (i.e. this) as well as the function I want to call (SubValueChanged).

The issue is that I'm not sure what the syntax is for doing this. i.e. how do I pass this to SubClass?

If I pass it MainClass this, then I have to include MainClass.h in SubClass.h, which isn't a good idea.

I'm guessing what I'm trying to do is quite a common C++/C task and I'm just trying to find the best way of doing it. :)

cheers Paul

Look at this, it might help you.

http://www.cplusplus.com/doc/tutorial/inheritance/

The first thing that you are doing wrong involves terminology. SubClass is in no way related to MainClass. So, calling it a sub-class is wrong.

If I pass it MainClass this, then I have to include MainClass.h in SubClass.h, which isn't a good idea.

No, you don't. You can put

class MainClass;

in SubClass.h, before the definition of the class mis-named SubClass.

In SubClass.cpp, you will need to include MainClass.h.

Hmm, fair point about the poor naming. ClassA and ClassB would have been more appropriate.

Think I get it now, thanks. This tutorial helped explain further too: http://www.newty.de/fpt/callback.html