root singleton w/o new
- LordMyth
- Gremlin
- Posts: 181
- Joined: Thu Jan 06, 2005 10:19 pm
- Location: Belgium
- Contact:
root singleton w/o new
Is it possible to get a pointer to the root object without a new operator?
- monster
- OGRE Community Helper

- Posts: 1098
- Joined: Mon Sep 22, 2003 2:40 am
- Location: Melbourne, Australia
- Contact:
Code: Select all
Root *ptrRoot = Root::getSingletonPtr();- LordMyth
- Gremlin
- Posts: 181
- Joined: Thu Jan 06, 2005 10:19 pm
- Location: Belgium
- Contact:
- monster
- OGRE Community Helper

- Posts: 1098
- Joined: Mon Sep 22, 2003 2:40 am
- Location: Melbourne, Australia
- Contact:
- epopov
- Halfling
- Posts: 85
- Joined: Tue Jun 10, 2003 2:57 pm
- Contact:
Ogre uses a singleton implementation which is somewhat different from the 'usual' implementation one can expect: in Ogre, you have to create your singleton with new before being able to use it. It's for some good reason, have a look here:
http://www.ogre3d.org/phpBB2/viewtopic. ... =singleton
http://www.ogre3d.org/phpBB2/viewtopic. ... =singleton
- LordMyth
- Gremlin
- Posts: 181
- Joined: Thu Jan 06, 2005 10:19 pm
- Location: Belgium
- Contact:
So I could use this to get around the new call?
Code: Select all
static Ogre::Root* GetRoot (void) {
static Ogre::Root _inst;
return &_inst;
}- monster
- OGRE Community Helper

- Posts: 1098
- Joined: Mon Sep 22, 2003 2:40 am
- Location: Melbourne, Australia
- Contact:
- LordMyth
- Gremlin
- Posts: 181
- Joined: Thu Jan 06, 2005 10:19 pm
- Location: Belgium
- Contact:
It works!
I'll explain... it is a way to use singletons. You have a function with a static root object, created with the normal method, like int i = 0;. The static object only gets created once, and the function allways returns the pointer to the root object. It's way better then new, because with this one, you don't have to call destructors, and you can allways access the address by just calling the function.
I'll explain... it is a way to use singletons. You have a function with a static root object, created with the normal method, like int i = 0;. The static object only gets created once, and the function allways returns the pointer to the root object. It's way better then new, because with this one, you don't have to call destructors, and you can allways access the address by just calling the function.
- sinbad
- OGRE Retired Team Member

- Posts: 19269
- Joined: Sun Oct 06, 2002 11:19 pm
- Location: Guernsey, Channel Islands
- x 67
- Contact:
It's infinitely simpler just to do:
Root gets constructed and destructed automatically with your app class and is always available through Root::getSingleton. Root is already a singleton, what you did was wrap another singleton idiom around it, specifically an auto-create idiom, which was completely unnecessary. We don't use the autocreate idiom because construction ordering is less predictable, and it doesn't support subclassing of singletons, something we do a lot (although not with Root).
The other problem with your solution is that you have absolutely no control over the destruction of Root, only the compiler has. That's the disadvantage of using statics.
Code: Select all
class App
{
protected:
Root mRoot;
...
The other problem with your solution is that you have absolutely no control over the destruction of Root, only the compiler has. That's the disadvantage of using statics.
- Kristian
- Hobgoblin
- Posts: 542
- Joined: Sun Jan 12, 2003 7:35 pm
- Location: Copenhagen, Denmark
my singleton class
this is the singlenton class i sometime use...
Code: Select all
template <typename T>
class Singleton {
public:
static T* getSingletonPtr(){
if (mInstance == 0)
mInstance = new T;
return mInstance;
}
static T* getSingleton() {
if (mInstance == 0)
mInstance = new T;
return mInstance;
}
static destroyInstance() {
delete mInstance;
mInstance = 0;
}
protected:
Singleton() {}
virtual ~Singleton() {}
private:
Singleton(const Singleton& source) { } //! Copy constructor.
static T* mInstance;
};
template <typename T> T* Singleton<T>::mInstance = 0;
- sinbad
- OGRE Retired Team Member

- Posts: 19269
- Joined: Sun Oct 06, 2002 11:19 pm
- Location: Guernsey, Channel Islands
- x 67
- Contact: