utiliser enable_shared_from_this::shared_from_this() dans le constructeur
Bonjour,
Mon bout de code ci dessous me pose problème :cry:.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13
| class IInteraction : public enable_shared_from_this<IInteraction> {
public:
IInteraction( boost::weak_ptr<CPointMechanicsEngine> iEngine ) :
_Engine( iEngine )
{
boost::shared_ptr<CPointMechanicsEngine> Engine(_Engine);
Engine->AddInteraction( shared_from_this() );
}
protected:
boost::weak_ptr<CPointMechanicsEngine> _Engine;
}; |
Le constructeur implémente le fait que IInteraction doit toujours être liée à un CPointMechanicsEngine et que si un IInteraction est lié à un CPointMechanicsEngine, celui ci a un lien vers lui.
Cependant, lors de l'appel au constructeur de IInteraction, shared_from_this() lève une exception car le pi du shared_ptr est nul. En effet, pn, px, pi ne sont pas initialisés.
Voici l'appel au constructeur, où CFriction est une classe qui dérive de IInteraction
Code:
1 2 3 4 5
| boost::shared_ptr<CFriction> Friction (
new Medias::PointMechanicsEngine::Interactions::CFriction(
PhysicalPoint ,
0.3 ,
Engine ) ); |
Si je fais appel à shared_from_this() dans une méthode après le constructeur ait terminé, il n'y a pas de problème.
Cependant, cela casse la RAII X(
Est ce que quelqu'un aurait une solution à mon problème, svp???
Merci!!!