tags:

views:

71

answers:

2

If one has a header file, let's say "test.h" including

namespace test
{
   enum ids
   {
      a = 1,
      b = 2,
      c = 3,
      d = 30
   };

   char *names[50];
};

and a source file, "test.cc" basically only including

test::names[test::a] = "yum yum";
test::names[test::c] = "pum pum";
// ...

Wouldn't it make more sense to wrap the implementation inside the namespace, too?

I'd say it would, as it's after all the implementation of the header file, so it would make sense to include the implementation in the same namespace as the header without manually prefixing every variable with test::, and prefix when using values from the outside.

This is the opinion of a C++ rookie, what would the smarter people say here?

+2  A: 

You can specify the namespace in test.cc as well. To do this, you would do something like:

#include "test.h"

namespace test
{
    ...
    names[a] = "yum yum"; 
    names[c] = "pum pum"; 
    ...
}

You could alternately use using as in:

#include "test.h"

using test;

...
names[a] = "yum yum"; 
names[c] = "pum pum"; 
...

I generally use the first method.

Edited by adding the ellipses (...) to account for the well founded objection chubsdad raised in his answer; I was a bit too hasty in answering this earlier and neglected, or took for granted certain syntactic and structural realities in C++.

andand
Yes, I know you could do that.. it was merely a question of "Is it good practice to do this or are you better off prefixing manually" :)
LukeN
@LukeN: As Lima Beans mentioned below, it's a matter of preference and consistency. I prefer, and would like to think that I consistently use the first method. I could make the case that being explicit (as you were in your code) is probably the most sound since it explicitly requires the developer to write what they mean rather than being more implicit. The downside is that it requires more typing and as such might bring about early arthritis of the wrist and fingers or possibly carpel tunnel syndrome ;)
andand
@LukeN: I too favor wrapping, note that the `using` method is frown upon (even in source files).
Matthieu M.
+2  A: 

I don't think it really matters, and I dont think there is global standard one way or the other. The important thing is to stay consistent with the rest of your codebase and do it the same way.

Personally, I prefer to wrap my implementation in the namespace like so:

namespace test
{
    names[a] = "yum yum"; 
    names[c] = "pum pum"; 
}

This seems to communicate to me that I am defining and implementing the test namespace, and not just "using" it.

Lima Beans