Re: Singletons...

From: Ivana Hrivnacova (Ivana.Hrivnacova@cern.ch)
Date: Wed Dec 01 1999 - 23:41:45 MET


 Hi Federico,

 The problem is in misinterpretation
 of the scope operator by cint;
 if you do:

root [6] AliSingleton* s = AliSingleton::Instance()
root [7] s->Data()=5                         
root [8] AliSingleton::Instance()->Data()
AliSingleton::Instance called.
(Int_t)5

 the data fData are modified as you wish

 but 
root [17]  AliSingleton::Instance()->Data()=8
root [18]  AliSingleton::Instance()->Data()
AliSingleton::Instance called.
(Int_t)5

 does not change the data. 
 I guess this is a task for Masaharu.

 Regards,

 Ivana 


==============================================================
e-mail:  Ivana.Hrivnacova@cern.ch
address: PPE Division; 160-1-016; CERN; 1211 Geneve; Switzerland
phone:   (022)-767-9749
Home institute: NPI Rez near Prague, Czech Republic
==============================================================

On Wed, 1 Dec 1999, Federico Carminati wrote:

> Hi All,
>   sorry for the lenghty message that follows. I could not make it any
> shorter. Ok, I am playing with singletons and I have the following
> implementation
> 
> rtest.h ----------------------------------------------------------------------
> 
> #include "TObject.h"
> #include <stdio.h>
> 
> class AliSingleton : public TObject
> {
> private:
>   static AliSingleton* fInstance;
>   Int_t fData;
> protected:
>   AliSingleton() {printf("Singleton creator called\n");}
> public:
>   static AliSingleton* Instance() {
>     if(!fInstance) fInstance = new AliSingleton;
>     //    fInstance->Data()=101;
>     return fInstance;
>   }
>   virtual ~AliSingleton() {
>     printf("Singleton distructor called %p\n",fInstance);
>   }
>   Int_t& Data() { return fData;}
>   void SetData(Int_t i) { fData=i;}
> 
>   ClassDef (AliSingleton,1)
> };
> 
> rtest.cxx --------------------------------------------------------------------
> 
> #include "rtest.h"
> 
> AliSingleton* AliSingleton::fInstance=0;
> 
> ClassImp(AliSingleton)
> 
> 
> 
> Nothing to be very proud of...
> 
> Now I have the following main program
> 
> #include <TROOT.h>
> #include <TFile.h>
> #include "rtest.h"
> 
> TROOT root("TROOT","Application");
> 
> int main () {
>   printf("Setting data\n");
>   AliSingleton::Instance()->Data()=101;
>   printf("data = %d\n",AliSingleton::Instance()->Data());
>   AliSingleton::Instance()->SetData(99);
>   printf("data = %d\n",AliSingleton::Instance()->Data());
>   AliSingleton::Instance()->SetData(98);
>   printf("data = %d\n",AliSingleton::Instance()->Data());
> }
> 
> which gives the following result
> 
> Setting data
> Singleton creator called
> data = 101
> data = 99
> data = 98
> 
> If I run the program as a macro instead, I get
> 
> Setting data
> Singleton creator called
> data = 0
> data = 99
> data = 98
> 
> That is the statement AliSingleton::Instance()->Data()=101; is not obeyed
> in the macro. Any idea? Thanks, 
> 
> 
> 
>                                             Federico Carminati
> 
> 
> 
> 



This archive was generated by hypermail 2b29 : Tue Jan 04 2000 - 00:43:43 MET