Archived

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

LuckyNewbie

Adding functions to a static base class doesn't work?

Recommended Posts

I''m using a "Singleton" base class in my project, a la Game Programming Gems, which is defined as follows:
// Singleton.h (thanks superpig!)

#ifndef _SINGLETON_H_
#define _SINGLETON_H_

#include <assert.h>

template<typename T>
class Singleton
{
   static T* ms_singleton;
   public:
      Singleton()
      {
         assert(!ms_singleton);
         //use a cunning trick to get the singleton pointing to the start of

         //the whole, rather than the start of the Singleton part of the object

         int offset = (int)(T*)1 - (int)(Singleton <T>*)(T*)1;
         ms_singleton = (T*)((int)this + offset);
      }
      ~Singleton()
      {
         assert(ms_singleton);
         ms_singleton=0;
      }
      static T& GetSingleton()
      {
         assert(ms_singleton);
         return *ms_singleton;
      }	  
////////////// THE NEW FUNCTION //////////////////

	  static bool Exists()
	  {
		  return false;
	  }
//////////////////////////////////////////////////

      static T* GetSingletonPtr()
      {
         assert(ms_singleton);
         return ms_singleton;
      }

};

template <typename T> T* Singleton <T>::ms_singleton = 0;

#endif
I use this class as a base class for any other class I want to be a singleton [re-read that sentence, it made sense!] However, a problem arose in the message loop, mainly the message WM_SIZE that (apparently) occurs when the window is created and every time it is resized. The problem is that I want to call a class through this singleton stuff, but if I haven''t instantiated the singleton yet, the assertion fails and my program crashes. I wanted to get around this by adding a function, Singleton::Exists(), which would test to see if ms_singleton exists, and if it doesn''t... don''t do anything with it. However, when I add this new function and try to use it on a typical statement, like ==================================== if (CMyClass::Exists()) CMyclass::GetSingleton().OnPaint(); ==================================== but my compiler gives me errors saying that "''Exists()'' is not a member of ''CMyClass''. But it is a member of CMyClass, I defined it to be a member of its base class Excuse me, Microsoft Visual C++, but... WHY IS THIS HAPPENING!!! Can anyone halp meh? Am I missing something elementary about inheritance? It would be appreciated. "Let me just ejaculate some ideas"

Share this post


Link to post
Share on other sites
Erm... nevermind, I seem to have identified my error a few minutes after I posted this. Funny how that works, eh? Anyways, for posterity [sic?], I was somehow managing to derive my class from a base class that it had no idea existed. I had #included "singleton.h", but singleton.h was nowhere to be found... so my class was derived from a class that [somehow] it knew about but didn''t know about... so making any errors in singleton.h would go unnoticed because it was simply not compiled. Stranger things have happened... but not many.

Share this post


Link to post
Share on other sites