0

I'm new to c++ and have a project with a handful of classes broken out into separate header and source files.

It would be very convenient for a few of these classes to have access to shared utility methods for validating keyboard input. This is because I don't want the class methods to be clogged up with long, identical code handing input stream/ buffer etc.

I found this answer recommending namespaces for this kind of problem, but I don't understand how to incorporate a namespace into my project.

For example, say I have

// Utility.h
namespace utility {
  method1() {...}
  method2() {...}
  etc..
}

is it improper to then do this:

// Foo.h
#include "Utility.h"
class Foo {
  void bar();
}

// Foo.cpp
#include "Foo.h"
void foo::bar {
  ..do stuff..
  utility::method1()
  ..do more stuff..
}

Where "Foo" may actually be a few classes identically using utility?

My intention: I'm interested to use utility methods only in the body of class methods to make the code more readable where identical checks are happening in multiple class methods.

Edit: added question I have referenced

3
  • Why would it be improper? What is the problem, exactly? Commented May 10, 2020 at 21:25
  • I want to know if there's a problem (compiler? style?) with this use of namespaces. The docs I've seen don't address namespaces as a way to do what I'm suggesting above (to my very untrained eye). Just trying to be careful. Commented May 10, 2020 at 21:27
  • Curious how else they'd be used? What is special about "this use" of namespaces? I don't really follow your concern. Commented May 10, 2020 at 21:29

2 Answers 2

2

Yes, you can define functions in a namespace, and yes you can then use those functions in different parts of your program, and yes that is how you call them.

No, there is no style or compiler problem with using namespaces.

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

2 Comments

I'm going to accept this answer once I can but can you tell me what the difference is between what I'm proposing and wrapping the class name in a namespace declaration like in the SE question I referenced?
@M-Wi Incidentally, you seem to have misread the linked page. The question had a class (only), and the accepted answer has a utility function in a namespace (only), just like you do here. There's no "wrapping the class name in a namespace declaration" anywhere there.
0

It is perfectly fine. You can even do:

void foo::bar {
  using namespace utility;
  method1():
  ..do more stuff..
}

In case a .cpp needs to include many headers (with their respective namespace), it helps your reader better understand which namespace are used in which of your functions.

The only stuff that you should not do is using namespace in your .h because by doing so you force anyone using your .h to also use the namespace you "used".

4 Comments

How does obscuring the use of namespaces "help your reader better understand which namespace are used in which of your functions"? I'd say quite the opposite.
What do you mean by obscuring the use of namespace?. If you need to refactor some of those function it is helping to know which functions they depend on (from which namespace those were taken). It might be obvious when only a few namespace are in use, not so obvious for tens of namespace.
Yeah except now you have to go through the entire function trying to work out which namespace goes with which function call. What did you gain?
Any IDE will tell you which function comes from which namespace if you need to know. The using at the beginning of the function is a helper for the writer (no need to specify the namespace before each function call) and a hint for whoever might need to refactor this function: all namespace used by this function declared at the beginning of the function. I have the feeling your comments are about the usage of using ... in general not the fact the "scope" of using is narrowed to a given function. This might deserve a dedicated post :-)

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.