Singletone Design Pattern
-
Hi !!! Can me somebody tell what is an singletone design pattern? Please give me some little code samle. Thanks.:cool: -:KNOX:-
-
Hi !!! Can me somebody tell what is an singletone design pattern? Please give me some little code samle. Thanks.:cool: -:KNOX:-
-
Hi !!! Can me somebody tell what is an singletone design pattern? Please give me some little code samle. Thanks.:cool: -:KNOX:-
search with this key word in code project you will get many. :) One example from CP: Singleton Pattern & its implementation with C++ --------------------------------------------------------------------
class my_singleton
{
private:
std::string my_string;
my_singleton() : my_string("foo bar bash") {}public:
static my_singleton &instance()
{
static my_singleton global_instance;
return global_instance;
}std::string get_string() { return my_string; }
};
...
cout << my_singleton::get_instance().get_string() << endl;Nice talking to you. :-O
If you judge people, you have no time to love them. -- Mother Teresa -
search with this key word in code project you will get many. :) One example from CP: Singleton Pattern & its implementation with C++ --------------------------------------------------------------------
class my_singleton
{
private:
std::string my_string;
my_singleton() : my_string("foo bar bash") {}public:
static my_singleton &instance()
{
static my_singleton global_instance;
return global_instance;
}std::string get_string() { return my_string; }
};
...
cout << my_singleton::get_instance().get_string() << endl;Nice talking to you. :-O
If you judge people, you have no time to love them. -- Mother TeresaHi, this class isn't really a singleton, because you can make more than one instances of it. You need to change the modifier of the constructor from public to at least protected. This way one is forced to use the instance function.
class my_singleton { private: std::string my_string; my_singleton() : my_string("foo bar bash") {} public: static my_singleton &instance() { static my_singleton global_instance; return global_instance; } std::string get_string() { return my_string; } }; ... cout << my_singleton::instance().get_string() << endl;
codito ergo sum
-
Hi, this class isn't really a singleton, because you can make more than one instances of it. You need to change the modifier of the constructor from public to at least protected. This way one is forced to use the instance function.
class my_singleton { private: std::string my_string; my_singleton() : my_string("foo bar bash") {} public: static my_singleton &instance() { static my_singleton global_instance; return global_instance; } std::string get_string() { return my_string; } }; ... cout << my_singleton::instance().get_string() << endl;
codito ergo sum
Oh. Sorry & Thanks for correcting me.:rose: Nice talking to you. :-O
If you judge people, you have no time to love them. -- Mother Teresa -
search with this key word in code project you will get many. :) One example from CP: Singleton Pattern & its implementation with C++ --------------------------------------------------------------------
class my_singleton
{
private:
std::string my_string;
my_singleton() : my_string("foo bar bash") {}public:
static my_singleton &instance()
{
static my_singleton global_instance;
return global_instance;
}std::string get_string() { return my_string; }
};
...
cout << my_singleton::get_instance().get_string() << endl;Nice talking to you. :-O
If you judge people, you have no time to love them. -- Mother TeresaThanks for the help. :cool: -:KNOX:-
-
Thanks for the help. :cool: -:KNOX:-
You are always welcome :rose: Nice talking to you. :-O
If you judge people, you have no time to love them. -- Mother Teresa