Jump to content
  • Advertisement
Sign in to follow this  
Tonin

static class vs. namespace

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

Here is the short form of my question: Is there a way to provide protection for variables and/or function in a namespace similar to "private" in a class? Long version: I'm working on some a code for a library that manages the screen resolution and color depth (for full screen mode and such), and have it all more or less working with several functions and some variables in a namespace. I would like to keep the user from messing with the internal variables though, so it seems that a static class would provide the functionality of a namespace as well as the protection of a class. Are there any downsides to this? It seems kind of weird to use a class like that, but I cant think of anything really wrong with it. Thanks for any ideas, ~Andy

Share this post


Link to post
Share on other sites
Advertisement
Why would these internal variables be in a header (and thus visible to the user) in the first place? If you feel paranoid about the variables being protected at the source level you can put them in an anonymous namespace (or declare them static).

Share this post


Link to post
Share on other sites
Global variables are only visible within the source file they're defined in by default. So unless you declare it extern within a header file, nobody else can mess with them.

CM

Share this post


Link to post
Share on other sites
I had considered just not putting them in the headers, but it seemed kind of sneaky and hackish. I suppose it would work well enough and be simple though.

Wouldn't just declaring them static still let users change the values of them? EDIT: Assumeing they were externed in a header.

I hadn't heard of anonymous namespaces before, I'll have to look in to that.

Thanks,
~Andy

Share this post


Link to post
Share on other sites
static global variables have internal linkage, which means unless the translation unit that declared the static global variable shares the address of the variable somehow with another variable then no other translation unit can access that variable.

Share this post


Link to post
Share on other sites
Ah, thanks for the clarification. It sounds like just not putting them in the headers would be the best then.

Thanks again,
~Andy

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Quote:
Original post by Tonin
I had considered just not putting them in the headers, but it seemed kind of sneaky and hackish. I suppose it would work well enough and be simple though.


Its not sneaky or hackish at all. C was designed this way for exactly this type of use. They realized that some things are simply not to be publicly available and they provided a mechanism for it. While C++ now provides better machanisms, you could esentially reproduce all the C++ data-hiding features (public, protected and private) with a clever use of headers.

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!