40

I already read a lot of posts and articles all over the net, but I couldn't find a definite answer about this.

I have some functions with similar purposes that I want to have out of the global scope. Some of them need to be public, others should be private (because they are only helper functions for the "public" ones). Additionally, I don't have only functions, but also variables. They are only needed by the "private" helper functions and should be private, too.

Now there are the three ways:

  • making a class with everything being static (contra: potential "Cannot call member function without object" - not everything needs to be static)
  • making a singleton class (contra: I WILL need the object)
  • making a namespace (no private keyword - why should I put it in a namespace at all, then?)

What would be the way to take for me? Possible way of combining some of these ways?

I thought of something like:

  1. making a singleton, the static functions use the helper function of the singleton object (is this possible? I'm still within the class, but accessing an object of it's type)
  2. constructor called at programm start, initializes everything (-> making sure the statics can access the functions from the singleton object)
  3. access the public functions only through MyClass::PublicStaticFunction()

Thanks.

3

6 Answers 6

42

As noted, using global variables is generally bad engineering practice, unless absolutely needed of course (mapping hardware for example, but that doesn't happen THAT often).

Stashing everything in a class is something you would do in a Java-like language, but in C++ you don't have to, and in fact using namespaces here is a superior alternative, if only:

  • because people won't suddenly build instances of your objects: to what end ?
  • because no introspection information (RTTI) is generated for namespaces

Here is a typical implementation:

// foo.h
#ifndef MYPROJECT_FOO_H_INCLUDED
#define MYPROJECT_FOO_H_INCLUDED

namespace myproject {
  void foo();
  void foomore();
}

#endif // MYPROJECT_FOO_H_INCLUDED

// foo.cpp
#include "myproject/foo.h"

namespace myproject {

namespace {
  typedef XXXX MyHelperType;

  void bar(MyHelperType& helper);
} // anonymous

void foo() {
  MyHelperType helper = /**/;
  bar(helper);
}

void foomore() {
  MyHelperType helper = /**/;
  bar(helper);
  bar(helper);
}
} // myproject

The anonymous namespace neatly tucked in a source file is an enhanced private section: not only the client cannot use what's inside, but he does not even see it at all (since it's in the source file) and thus do not depend on it (which has definite ABI and compile-time advantages!)

Sign up to request clarification or add additional context in comments.

14 Comments

Thanks. I decided this as the best solution for my problem. I'm coming from C# (where everything is a class), that's why these (unnamed) namespace solutions are new to me.
@blubberbernd: in this case I can only recommend a good C++ book. C++ is both rich and full of (bad) surprises, and a good tutorial can only help. There is a list on SO: stackoverflow.com/questions/388242/…
Nice answer. I am doing some embedded programming in C++ and was confronted with the problem that some peripherals just can be used (should be initialized) once. It was a natural thing to implement in C but I felt I HAD TO go for an object oriented approach using C++. But using a singleton was of limited use because I also had to implement related interrupt service routines as "free functions".
One question about the anonymous namespace: If the "private" functions aren't declared in the header, wouldn't that be enough to prevent client code from using them? Am I missing something?
@Rev1.0: Anonymous namespaces and static are there for linkage, primarily: the symbol is then not visible outside the translation unit in which it is defined, which avoids collisions. It also hints to the compiler that it need not emit the function code at all and might want to consider more strongly inlining it. Incidentally, it also prevents someone who has peeked into the source code from declaring the function so as to use it too: to use it they need to somehow inspect the binary to find its address... if it exists at all (ie, was not inlined).
|
12

Don't make it a singleton

For public helper functions that don't directly depend on these variables, make them non-member functions. There's nothing gained by putting them in a class.

For the rest, put it in a class as normal non-static members. If you need a single globally accessible instance of the class, then create one (but don't make it a singleton, just a global).

Otherwise, instantiate it when needed.

6 Comments

Yay, anonymous downvotes. Anyone care to explain why this is a bad answer?
I'm not the downvoter, but I'm guessing some might take issue with your "globally accessible instance" paragraph. Global objects suffer the same disadvantages as singletons (as singletons are merely global objects in disguise).
Another point of contention might be "There's nothing gained by putting them in a class." There is one thing gained, albeit trivial: the class that holds the static member functions serves as a namespace. I realize that the same effect can be accomplished by namespace, but perhaps people coming from a Java background don't.
@Emile: no they don't. Read the blog post I linked to. Singletons combine two different problems. A plain old global has only one of them. That's still enough to make it a bad solution most of the time, but I'd say a global has a few legitimate uses. A singleton has none.
On your second point, as you say, a namespace is better for that purpose.
|
2

The classic C way of doing this, which seems to be what you want, is to put the public function declarations in a header file, and all the implementation in source file, making the variables and non-public functions static. Otherwise just implement it as a class - I think you are making a bit of a mountain out of a molehill here.

4 Comments

Note that the use of static this way is deprecated in C++ in favor of anonymous namespaces. So namespace { int i; } rather than static int i; Otherwise, yes.
@Dennis Stroustrup was talking about undeprecating it some time ago, which I think is the right thing to do. Don't know if anything ever came of it. I find the nameless namespace thing, well ... silly.
Personally, I find static having three completely unrelated meanings silly. But looking through the C++0x draft, they seem to have removed that bit. So nevermind :-)
1

What about using a keyword static at global scope (making stuff local to the file) as a privacy substitute?

Comments

1

From your description it looks like you have methods and data that interact with each other here, in other words it sounds to me like you actually want a non-singleton class to maintain the state and offer operations upon that state. Expose your public functions as the interface and keep everything else private.

Then you can create instance(s) as needed, you don't have to worry about init order or threading issues (if you have one per thread), and only clients that need access will have an object to operate upon. If you really need just one of these for the entire program you could get away say a global pointer that's set in main or possibly an instance method, but those come with their own sets of problems.

Comments

0

Remember that the singleton instance of a singleton class is a valid instance, so it is perfectly able to be the recipient of nonstatic member functions. If you expose your singleton factory as a static function then have all of your public functionality as public nonstatic member functions and your private functionality as private nonstatic member functions, anyone that can get at the class can access the public functionality by simply invoking the singleton factory function.

You don't describe whether all of the functionality you're trying to wrap up is as related as to justify being in the same class, but if it is, this approach might work.

If you take a "C-like" approach and just use top-level functions, you can make them private by declaring them in the .cpp file rather than the publicly-included .h file. You should also make them static (or use an anonymous namespace) if you take that approach.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.