Jump to content
  • Advertisement
Sign in to follow this  
thedodgeruk

is there a way to create a static class

This topic is 3088 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

im writing a basic game engine , the input class that deals with keyboard and mouse i want that to be static so , it can only be created once . is there a way , or is it a case of just dont try to create more

Share this post


Link to post
Share on other sites
Advertisement
There are ways - one way is a singleton class. But it brings with it its own set of problems as well. Another option is as you said don't create more than one which also brings its own set of problems. There probably are a few more but I will let you search for them :). All have certain pros and cons - in the end you should make an informed decision on what you choose and why.

Share this post


Link to post
Share on other sites
Since you haven't stated the language you are using I'm going to assume it is C++.

Sure there is, it's called a singleton, the trouble is it's not a very good design pattern to utilise as more often than not alternative designs can be utilized for the same effect, inexperienced programmers will try to argue their way into a hole that it must be a singleton as they only need one, when it's a pointless restriction which can be worked around with thought out design, there are cases when singleton's are appropriate but those are few and far between in my opinion.

Honestly you are better off simply not creating more than one instance of the input class, one way you could restrict programmers from creating more than one is "embedding" it into the core class like so:


class EngineCore
public:
EngineCore();
~EngineCore();

InputManager input;
};


By limiting the program to using the EngineCore.input as it's input manager regardless of how many InputManager's the programmer instantiates there can only be one used at any one time. In this example I made the InputManager public in order to allow the user to assign to it, as long as member functions of the EngineCore explicitly use the InputManager member it could be a workable solution.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

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

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!