C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

classe singleton Vs namespace et variable globale

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ (French)
View previous topic :: View next topic  
Author Message
Aurélien REGAT-BARREL
Guest





PostPosted: Fri Jul 16, 2004 3:40 pm    Post subject: classe singleton Vs namespace et variable globale Reply with quote



Hello, c'est encore moi.
J'ai besoin de gérer une valeur au sein du logiciel. J'étais parti sur une
classe singleton (je n'ai besoin que d'une instance), mais je me demande ce
qui est le mieux entre :

// classe singleton classique
class Singleton
{
public:
static Singleton & GetInstance();

int GetValue() const { return this->value; }

private:
Singleton();
Singleton( const Singleton & );
Singleton & operator=( const Singleton & );

private:
int value;
};

=> int val = Singleton::GetInstance().GetValue();

// membres statiques public uniquement
class Singleton2
{
public:
static int GetValue() { return GetInstance().value; }

private:
Singleton2();
static Singleton2 & GetInstance();

private:
int value;
};

=> int val = Singleton2::GetValue();

// namespace
namespace Singleton3
{
// .h
int GetValue();

// .cpp
namespace // anonyme
{
int value;
}
int GetValue()
{
return value;
}
}

=> int val = Singleton3::GetValue();

Que préférez-vous entre Singleton, Singleton2 et Singleton3 ?
Merci.

--
Aurélien REGAT-BARREL


Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ (French) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.