|
|||||||
|
|
|
![]() |
|
|
Strumenti |
|
|
#1 |
|
Senior Member
Iscritto dal: Oct 2006
Città: milano
Messaggi: 1439
|
[C++] pattern singleton
ciao a tutti con l'aiuto del libro desing patterns stavo provando a guardare qualcosa.. volevo iniziare dal pattern singleton che mi sembra piuttosto semplice ma ho già avuto le prime difficoltà.. i codici sono questi:
Singleton.h Codice:
#ifndef SINGLETON_H
#define SINGLETON_H
class Singleton
{
public:
static Singleton* Instance();
protected:
Singleton();
private:
static Singleton* _instance;
};
#endif
Codice:
#include <iostream>
#include "Singleton.h"
Singleton* Singleton::_instance = 0;
Singleton* Singleton::Instance()
{
if(_instance == 0)
_instance = new Singleton;
else
std::cout << "another instance of singleton can't be instanced" << std::endl;
return _instance;
}
Codice:
#include <iostream>
#include "Singleton.h"
int main()
{
Singleton* a = Singleton.Instance();
Singleton* b = Singleton.Instance();
Singleton* c = Singleton.Instance();
return 0;
}
dove sbaglio? |
|
|
|
|
|
#2 |
|
Senior Member
Iscritto dal: Mar 2009
Città: Bologna
Messaggi: 1174
|
La dichiarazione e':
static Singleton* Instance(); quindi: Singleton* a = Singleton::Instance(); |
|
|
|
|
|
#3 |
|
Registered User
Iscritto dal: May 2005
Città: far away from home
Messaggi: 1038
|
A parte il tuo errore.
Ricordati di non dare alle variabili nomi che iniziano per underscore. |
|
|
|
|
|
#4 |
|
Senior Member
Iscritto dal: Jul 2005
Città: Bologna
Messaggi: 1130
|
E perchè?
__________________
-> The Motherfucking Manifesto For Programming, Motherfuckers |
|
|
|
|
|
#5 |
|
Registered User
Iscritto dal: May 2005
Città: far away from home
Messaggi: 1038
|
11. Style: Try to avoid names with leading underscores. Yes, I've habitually used them, and yes, popular books like "Design Patterns" (Gamma et al) do use it... but the standard reserves some leading-underscore identifiers for the implementation and the rules are hard enough to remember (for you and for compiler writers!) that you might as well avoid this in new code.
Da Exceptional C++ di Sutter. |
|
|
|
|
|
#6 | |
|
Senior Member
Iscritto dal: Jul 2005
Città: Bologna
Messaggi: 1130
|
Quote:
... http://en.wikipedia.org/wiki/Fashion_victim
__________________
-> The Motherfucking Manifesto For Programming, Motherfuckers |
|
|
|
|
|
|
#7 |
|
Senior Member
Iscritto dal: Oct 2006
Città: milano
Messaggi: 1439
|
grazie ora funziona, non avevo mai chiamato un metodo statico in C++.. sbaglio o negli altri linguaggi si fa come avevo scritto io??
|
|
|
|
|
|
#8 | |
|
Registered User
Iscritto dal: May 2005
Città: far away from home
Messaggi: 1038
|
Quote:
|
|
|
|
|
|
| Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 10:58.




















