Jump to content

  • Log In with Google      Sign In   
  • Create Account


Static Classes in c++


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
13 replies to this topic

#1 Joshhua5   Members   -  Reputation: 436

Like
0Likes
Like

Posted 16 May 2014 - 09:26 PM

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);
		}; 
}

 


Edited by Joshhua5, 16 May 2014 - 10:52 PM.


Sponsor:

#2 frob   Moderators   -  Reputation: 20355

Like
1Likes
Like

Posted 16 May 2014 - 10:02 PM

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?
Check out my personal indie blog at bryanwagstaff.com.

#3 Joshhua5   Members   -  Reputation: 436

Like
0Likes
Like

Posted 16 May 2014 - 10:18 PM

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


Edited by Joshhua5, 16 May 2014 - 10:20 PM.


#4 frob   Moderators   -  Reputation: 20355

Like
0Likes
Like

Posted 16 May 2014 - 10:20 PM

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.
Check out my personal indie blog at bryanwagstaff.com.

#5 Joshhua5   Members   -  Reputation: 436

Like
0Likes
Like

Posted 16 May 2014 - 10:39 PM

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.


Edited by Joshhua5, 16 May 2014 - 10:40 PM.


#6 h3xl3r   Members   -  Reputation: 156

Like
0Likes
Like

Posted 16 May 2014 - 10:43 PM

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!

#7 Joshhua5   Members   -  Reputation: 436

Like
0Likes
Like

Posted 16 May 2014 - 10:52 PM

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.



#8 ApochPiQ   Moderators   -  Reputation: 15091

Like
2Likes
Like

Posted 16 May 2014 - 10:55 PM

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).

Edited by ApochPiQ, 16 May 2014 - 10:57 PM.


#9 h3xl3r   Members   -  Reputation: 156

Like
0Likes
Like

Posted 16 May 2014 - 11:00 PM

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.

#10 Joshhua5   Members   -  Reputation: 436

Like
0Likes
Like

Posted 16 May 2014 - 11:05 PM

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.



#11 h3xl3r   Members   -  Reputation: 156

Like
1Likes
Like

Posted 16 May 2014 - 11:14 PM

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;

Edited by h3xl3r, 17 May 2014 - 12:02 AM.


#12 L. Spiro   Crossbones+   -  Reputation: 13306

Like
0Likes
Like

Posted 16 May 2014 - 11:54 PM

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
It is amazing how often people try to be unique, and yet they are always trying to make others be like them. - L. Spiro 2011
I spent most of my life learning the courage it takes to go out and get what I want. Now that I have it, I am not sure exactly what it is that I want. - L. Spiro 2013
I went to my local Subway once to find some guy yelling at the staff. When someone finally came to take my order and asked, “May I help you?”, I replied, “Yeah, I’ll have one asshole to go.”
L. Spiro Engine: http://lspiroengine.com
L. Spiro Engine Forums: http://lspiroengine.com/forums

#13 h3xl3r   Members   -  Reputation: 156

Like
0Likes
Like

Posted 17 May 2014 - 12:48 AM

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.

Edited by h3xl3r, 17 May 2014 - 12:50 AM.


#14 rip-off   Moderators   -  Reputation: 8120

Like
1Likes
Like

Posted 17 May 2014 - 05:26 AM

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.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS