Static Classes in c++

Started by
12 comments, last by rip-off 9 years, 11 months ago

It was just according to the Singleton pattern there's a static function that returns a instance of the class, which is then called upon.
Instead of mine which is calling static functions of the class.


Yes, that's a choice. You can go both ways.

1) Make all functions + members static
2) Make only the "GetInstance" method static and use the instance it returns to call the non-static member functions. The point is that you can call "GetInstance()" on the class without an instance, thus no need to pass the instance around.

Here's your class definition changed to be a Singleton:
namespace Pro
{
	typedef unsigned int GUID; 

	class GUIDLookup 
	{   
	private:

		GUIDLookup(); // constructor is private
		static GUIDLookup* mInstance; // private instance


	public: 
					 
		static GUIDLookup* GetInstance()
		{
			if(!mInstance) mInstance = new GUIDLookup();
			return mInstance;
		}

		// all other non-static public methods
		// can be called after getting the instance with the method above
		GUID newGUID(const std::string& name);
		void releaseGUID(const std::string& name);
		void releaseGUID(GUID);
		GUID getGUIDFromName(const std::string& name);
		std::string getName(GUID _id);

	}; 

}
Edit: Oh and you'll have to intialize the mInstance variable to 0/NULL in a static initializer in the implementation file like so (and a better name would probably be msInstance, but it's too early in my timezone smile.png :

GUIDLookup* GUIDLookup::mInstance = 0;
Advertisement

I'm trying to work on my class structure and naming, what's why I keep refactoring my project tongue.png

In this regard only, several things stand out to me. I’ve outlined them here.
#1: Why is “class GUIDLookup{” one tab deeper than the line above it? The 2 lines are unrelated, and indentation is unquestionable in its value.
#2: Public, protected, then private, in that order. No exceptions.
#3: Search my link for “Class functions look much nicer when they are all indented the same amount.”
LSVOID LSE_CALL SetAllocator( CAllocator * _paAllocator );
LSVOID LSE_CALL Reset();
LSE_INLINE LSUINT32 LSE_CALL GetWidth() const;
LSE_INLINE LSUINT32 LSE_CALL GetHeight() const;
LSE_INLINE LSI_PIXEL_FORMAT LSE_CALL GetFormat() const;
LSE_INLINE const LSVOID * LSE_CALL GetBufferData() const;
LSVOID LSE_CALL                        SetAllocator( CAllocator * _paAllocator );
LSVOID LSE_CALL                        Reset();
LSE_INLINE LSUINT32 LSE_CALL           GetWidth() const;
LSE_INLINE LSUINT32 LSE_CALL           GetHeight() const;
LSE_INLINE LSI_PIXEL_FORMAT LSE_CALL   GetFormat() const;
LSE_INLINE const LSVOID * LSE_CALL     GetBufferData() const;

L. Spiro

I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid

About structure for this specific problem:

In this case I would not make this class a singleton either. As there's usually some sort of "Game/Application" class already that's responsible for creating instances of all subsystems on start-up, and seeing as this particular class is naturally a singleton as there can be only ever one instance of it, that is a good place to just create and put getters for your "global tools" there.

So you'd make the master "Game" class create an instance of your GUIDManager once and have a GetGUIDManager getter to let all other subsystems have access to it via "Game".

Sure it could all be structured differently, but this way you avoid making a lot of singletons (bad!) and using the one you already "have to" make anyway.
Lua has a couple of mechanisms that allow you to avoid this. I believe in this case you might want to use the registry, though you could also use upvalues too.

This allows you to have a global value in the Lua state, but not in your C++ code. This could be beneficial - for example, if you were writing a multiplayer game, you might want to run the server and client in the same process. Unlike solutions that involve C++ global values, such an implementation would allow two independent GUIDLookup instances to be created that cannot interfere with one another.

As ApochPiQ mentions, it is common to use a Lua wrapper library that will handle these kind of details for you. You should definitely consider doing this if you haven't already explored such options.

This topic is closed to new replies.

Advertisement