Jump to content
  • Advertisement
Sign in to follow this  
PureSnowX

Question about good practice and static member variables

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

As the topic prefix tells you, this is regarding C++.

I recently started working on a bitmap-font class which loads a font, and creates texture triangles a containd draws a BitString ( a struct with string, x and y coords ).

I decided to make it an underlying class for a new class called BitText which is basically a Bitmap-font string, it has all the fancy features of searching for '\n' and you have assign() and append(). This makes it extremely easy to make a bitmap-font text and display it:
 

BitText bt("Hello World",0,0);
bt.draw(render_window);

 

Seeing as all instances shares ( and needs ) a Bitmap-font I decided to make it a private static member of the BitText class.

Is this a good way of using static members? If it works it works, but now I'm asking for good practice advice.

Share this post


Link to post
Share on other sites
Advertisement
Is it conceivable you may want to support different fonts in the future? This seems so natural that I would actually implement this possibility right away, by giving your BitText class a [shared?] pointer to the "bitmap font" class.
BitText bt("Hello World",0,0,default_font);
bt.draw(render_window);

Share this post


Link to post
Share on other sites

Try instantiating BitText in main, then pass it around where needed. That class seems to be lightweight right? So I assume creating that object is light.

Share this post


Link to post
Share on other sites

None of you answered my question at all. I was asking about private static member variables ( by giving a context ) and both of you went ahead and answered a question I didn't ask.

BitText is like a std::string object, only it has a draw function to go along with it. BitText doesn't actually draw anything by itself, it passes it's BitStrings ( a line of text with a position ) into the static member variable BitmapFont::draw(render_window, bitstring);

Share this post


Link to post
Share on other sites
The question as originally posed ("Is this a good way of using static members?") is not very interesting. It suggests that using static members is an end in and of itself, and your code is a way to satisfy that end. Of course, this is nonsense. Instead, we substituted the more interesting question (something like "Should I use a static member in this situation?") and answered it.

Programming is about solving problems, not about finding opportunities to use language constructs. I don't think I have ever used a private static member either, and I feel no need to do so.

Share this post


Link to post
Share on other sites

I would not consider this a good use of static members. For a concrete argument against, the BitText class will be problematic to unit test due to the hidden global dependency.

Share this post


Link to post
Share on other sites
Making it private and static raises questions:
What ensures it is only initialised once?
Will it cause problems in a multi-threaded scenario?

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!