The compiler doesnt find immediately function called initialize
Selected C++ Topics 937
void RunGame(void) {
// initialize 3D engine
MyCool3DEngine::Initialize(); // etc.
}
|
---|
using namespace MyCool3DEngine;
void RunGame(void) {
This works similar to how a search path works for files. The using namespace line tells the compiler that if it can’t find a function, it should look for it within the specified namespace. In the previous example, if the compiler doesn’t find immediately a function called Initialize, it looks in the namespace MyCool3DEngine.
The using namespace feature is handy, not only because it saves you some keystrokes, but also because if the name of the namespace changes, you only have to adjust the code in one place (that is, you only have to change the using namespace line).
938 | B. |
---|
|
---|
You can also nest namespaces. Think of the scope resolution operator, ::, as the equivalent to the backslash in the file system (it’s not quite as versatile, but it’s a good analogy for right now). For example, let’s say you have name-space A embedded in namespace B, like so:
namespace A {
You could get to Foo by writing:
A::B::Foo();
|
---|
|
---|