root singleton w/o new

Problems building or running the engine, queries about how to use features etc.
Post Reply
User avatar
LordMyth
Gremlin
Posts: 181
Joined: Thu Jan 06, 2005 10:19 pm
Location: Belgium
Contact:

root singleton w/o new

Post by LordMyth »

Is it possible to get a pointer to the root object without a new operator?
User avatar
monster
OGRE Community Helper
OGRE Community Helper
Posts: 1098
Joined: Mon Sep 22, 2003 2:40 am
Location: Melbourne, Australia
Contact:

Post by monster »

Code: Select all

Root *ptrRoot = Root::getSingletonPtr();
:?:
User avatar
LordMyth
Gremlin
Posts: 181
Joined: Thu Jan 06, 2005 10:19 pm
Location: Belgium
Contact:

Post by LordMyth »

Oh ehh THANKS! But why is everybody using new?
[EDIT]
It says segmentation fault when I try to run the program... (It's Robomaniacs tutorial)
User avatar
monster
OGRE Community Helper
OGRE Community Helper
Posts: 1098
Joined: Mon Sep 22, 2003 2:40 am
Location: Melbourne, Australia
Contact:

Post by monster »

Haven't seen the tutorial, but obviously you need to new it before you can get a pointer to the Singleton.

Why are you so opposed to new, if that's what the tutorial tells you to do?
User avatar
epopov
Halfling
Posts: 85
Joined: Tue Jun 10, 2003 2:57 pm
Contact:

Post by epopov »

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
User avatar
LordMyth
Gremlin
Posts: 181
Joined: Thu Jan 06, 2005 10:19 pm
Location: Belgium
Contact:

Post by LordMyth »

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;
		}
User avatar
monster
OGRE Community Helper
OGRE Community Helper
Posts: 1098
Joined: Mon Sep 22, 2003 2:40 am
Location: Melbourne, Australia
Contact:

Post by monster »

No. But why do you want to do this anyway?

You have to create a Root object, the example frameworks typically do that for you, I'm guessing that Robo's tutorials don't use this framework. But you still have to do it with 'new', why are you so opposed to this?
User avatar
LordMyth
Gremlin
Posts: 181
Joined: Thu Jan 06, 2005 10:19 pm
Location: Belgium
Contact:

Post by LordMyth »

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.
User avatar
sinbad
OGRE Retired Team Member
OGRE Retired Team Member
Posts: 19269
Joined: Sun Oct 06, 2002 11:19 pm
Location: Guernsey, Channel Islands
x 67
Contact:

Post by sinbad »

It's infinitely simpler just to do:

Code: Select all

class App
{
protected:
    Root mRoot;
...
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.
User avatar
Kristian
Hobgoblin
Posts: 542
Joined: Sun Jan 12, 2003 7:35 pm
Location: Copenhagen, Denmark

my singleton class

Post by Kristian »

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;
Kristian, crying out loud: "If it works. Don't fix it!" :p
HCA
HCA2
GuppyLife
User avatar
sinbad
OGRE Retired Team Member
OGRE Retired Team Member
Posts: 19269
Joined: Sun Oct 06, 2002 11:19 pm
Location: Guernsey, Channel Islands
x 67
Contact:

Post by sinbad »

Yeah, that's another auto-creator which we don't use for reasons I've already stated ;) It would be impossible for, say TextureManager/GLTextureManager to transparently exist as a singleton using an auto-create set up.
Post Reply