Jump to content
  • Advertisement


This topic is now archived and is closed to further replies.

Austrian Coder

Whats wrong whit that?

This topic is 5880 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

#ifndef _SINGLETON_H_ #define _SINGLETON_H_ #include "n_globals.h" // global includes namespace XXXXXXX { template class Singleton { private: static T *m_Singleton; public: Singleton() { if(m_Singleton != NULL) { // todo: error handling } int offset = (int)(T*)1 - (int)(Singleton *)(T*)1; m_Singleton = (T*)((int)this + offset); } // ================================== ~Singleton() { if(m_Singleton) { delete m_Singleton; } m_Singleton = NULL; } // ================================== inline static bool IsValid() { return(NULL != m_Singleton); } // ================================== inline static T& GetInstance() { if(NULL == m_Singleton) { static T typeInstance; m_Singleton = &typeInstance; } return(*m_Singleton); } }; // ================================== template T* Singleton ::m_Singleton = 0; // ================================== } #endif /*_SINGLETON_H_*/ Whats wrong with this? When i use it like this: class COpenGL : public Singleton ... and then COpenGL *OGL = &COpenGL::GetInstance() Here is a screenshot from vc in debugmode! http://www.visual-page.de/singelton.jpg As you can see my source produces alot of instances... Thanks PS: sorry for the black lines in the picture!

Share this post

Link to post
Share on other sites
Wow, where did you learn about singletons? I think you''re rather confused...

For those that are iterested, the class definition for COpenGL looks like this:

class COpenGL : public Singleton<COpenGL>

The browser just strips out the < / >...

Anyway, there''s a lot of reasons why that doesn''t work, and I would recommend you get yourself a copy of the book "Design Patterns" (I forget the author, just do a search on amazon.com). Failing that, find a good tutorial - a search on google should give you a plethora to choose from.

If I had my way, I''d have all of you shot!

codeka.com - Just click it.

Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!