Static Classes in c++

Started by
12 comments, last by rip-off 10 years ago

I've been passing an GUIDManager around in my project to where ever it's been needed, this was causing some problems. I was wondering if making the whole class static would cause any issues? Before I start correcting all reference to this class, I want to make sure that I won't come into some issue in the future.


namespace Pro{
	typedef unsigned int GUID; 
		class GUIDLookup{   
			static unsigned int allocatedBitCount;
			static std::unordered_map<std::string, GUID> nameMapping;
			static std::stack<unsigned int> releasedGUIDs;
			static GUID newGUID();
		public: 
			 
			static GUID newGUID(const std::string& name);
			static void releaseGUID(const std::string& name);
			static void releaseGUID(GUID);
			static GUID getGUIDFromName(const std::string& name);
			static std::string getName(GUID _id);
		}; 
}

Advertisement
What is your real problem you are trying to solve? You don't want to pass an object around?

There are reasons to mark a function as static, usually when you have a reason not to need the this pointer.

There are times it is a solution to problems, but with the very brief description, I don't think that is the answer. I think the deeper problem is the "manager" class not having a single responsibility. Most "manager" classes are a symptom of a design flaw.

In this case your "manager" looks like a name/id lookup. Is there more to the class?

I was cleaning up my code and thought one way would be to stop passing around the GUIDManager.

You're right the it is a name/id lookup.

I've also decided to change the name to GUIDLookup

Looking over your other posts on trying to get Lua objects to work with game objects, I'm guessing this "manager" class actually belongs as a part of a larger class. It probably belongs as part of a resource cache system. A resource cache can load and unload objects if needed, find the objects if they exist, and potentially (depending on how you want to do it) load up a proxy while you wait for the resources to load. One facet of a resource cache is to translate names into object pointers.

When you try to name a class, the name "manager" is usually a clear marker that you aren't sure about the classes responsibilities. Sometimes the class is really a subset of another class responsibility, sometimes it is a collection of too many responsibilities.

Of course, if this bit of code has nothing to do with those earlier discussions then it could be part of a totally different system, but the concern of a generic "manager" rather than a specific purpose still remains.

Well as of right now, Lua will refer to an object via it's name, but before Lua performs a required task, it'll get the ID of the name and use when it operates on the C++ code.

The only problem is that the functions that Lua calls at times require the GUIDLookup. All functions that Lua calls must be static, which means I either have to pass the GUIDLookup via Lua's stack or have the GUIDLookup static.

There is no such thing as a "static class". You can make member functions static but these will only be able to access static member variables. To make the "whole" class accessible from anywhere in the program use the singleton pattern http://en.wikipedia.org/wiki/Singleton_pattern : You add one static method only to return the instance.

Use this sparingly! Too many of these is a sign of a bad class structure really ( as is the use of the word "manager" in class-names ;) though there are always exceptions if you have a valid reason!

I'm trying to work on my class structure and naming, what's why I keep refactoring my project :P

What would you call my example up above? because it doesn't fit into a Singleton.

I've heard the term Monostate used for similar things.

Personally I don't like singleton or monostate implementations much. If you have some code that needs GUID mapping lookup, just build a lightweight mapping wrapper that you can use as a component (i.e. member variable) of any object that needs that lookup mapping.


For your Lua code, I'd suggest setting up the mapping in your Lua context wrapper (assuming you did it right and you have one!) and then just use a thunk (standalone function that can find your Lua data, including the GUID mappings, and do whatever).

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

I'm trying to work on my class structure and naming, what's why I keep refactoring my project :P

What would you call my example up above? because it doesn't fit into a Singleton.


I see no reason why this class would not work as a singleton. In fact, having the word manager in the name almost indicates it is a singleton, which is why it makes people sense there's some problem as you should have as little as possible of these.

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.

This topic is closed to new replies.

Advertisement