PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.cplus > Pointer back to a specific instance.
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Pointer back to a specific instance.

Réponse
 
LinkBack Outils de la discussion
Vieux 29/12/2007, 17h01   #1
TBass
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Pointer back to a specific instance.

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


  Réponse avec citation
Vieux 29/12/2007, 17h27   #2
TBass
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Pointer back to a specific instance.

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;



}
  Réponse avec citation
Vieux 29/12/2007, 17h52   #3
Salt_Peter
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Pointer back to a specific instance.

On Dec 29, 12:01 pm, TBass <t...@automateddesign.com> wrote:
> 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 full 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
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 12h32.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,69110 seconds with 11 queries