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