Pointer back to a specific instance.

T

TBass

Hi,

I want to find out the best "C++" way to do this:

I have 2 classes:

class IHost:
{

static callback(..)
}


and


#include "IHost.h"
class PluginManager:
{
....

protected:
IHost m_IHost;
}


For one function, the IHost class has to signal back to the
PluginManager class that the plugin has signalled a status change.
Before I get into what I tried, I want to first ask, what is the
proper "C++ way" of doing such a circular reference? Friend class?

Anyway, I tried this:

#include "PluginManager.h"
class IHost:
{
IHost ( PluginManager *ptr )
{
m_pPluginManager = ptr;
}

static callback(..)

protected:
PluginManager *m_pPluginManger
}


and


#include "IHost.h"
class PluginManager:
{
....

protected:
IHost m_IHost;
}

However, my compiler still won't recognize PluginManager as a class in
the IHost class definition. I've had a problem in the past that if I
try to include in a class a class that is already including that class
(like the example directly above), the compiler barfs in C++.

My next thought was to try a friend class definition, but then I
figrued I would try and find out the proper way to do this.

Thanks for any feedback in advance.

T
 
T

TBass

Nevermind, I think I figured it out:

class PluginManager;

class IHost:
{
IHost ( PluginManager *ptr )
{
m_pPluginManager = ptr;
}


static callback(..)


protected:
PluginManager *m_pPluginManger



}


and

#include "IHost.h"
class PluginManager:
{
....


protected:
IHost m_IHost;



}
 
S

Salt_Peter

Hi,

I want to find out the best "C++" way to do this:

I have 2 classes:

class IHost:
{

static callback(..)

}

and

#include "IHost.h"
class PluginManager:
{
....

protected:
IHost m_IHost;

}

For one function, the IHost class has to signal back to the
PluginManager class that the plugin has signalled a status change.
Before I get into what I tried, I want to first ask, what is the
proper "C++ way" of doing such a circular reference? Friend class?

Anyway, I tried this:

#include "PluginManager.h"
class IHost:
{
IHost ( PluginManager *ptr )
{
m_pPluginManager = ptr;
}

static callback(..)

protected:
PluginManager *m_pPluginManger

}

and

#include "IHost.h"
class PluginManager:
{
....

protected:
IHost m_IHost;

}

However, my compiler still won't recognize PluginManager as a class in
the IHost class definition. I've had a problem in the past that if I
try to include in a class a class that is already including that class
(like the example directly above), the compiler barfs in C++.

it should barf
My next thought was to try a friend class definition, but then I
figrued I would try and find out the proper way to do this.

Thanks for any feedback in advance.

T

Its always helpfull if you post compileable code, even if its toy
code.
Use initialisation lists in your constuctors, other languages would
kill to have them.
Your problem involves forward declarations:

// forward declaration
class PluginManager;

class IHost
{
PluginManager* m_pPluginManger;
public:
IHost(PluginManager* ptr) : m_pPluginManger(ptr)
{
}
static void callback() { }
};

class PluginManager
{
public:
PluginManager() : m_IHost(this)
{
}
protected:
IHost m_IHost;
};

int main()
{
PluginManager pm;
}

As far as callbacks are concerned, take a look at Boost.bind,
Boost.Function
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,181
Messages
2,570,970
Members
47,536
Latest member
VeldaYoung

Latest Threads

Top