I have declared a class X in X.h as follows:
namespace Foo {
class X{
....
};
}
In X.cc I would like to define the constructors, methods for X.
Do I need to enclose all my definitions inside namespace Foo {...}
or prefix X as Foo::X:: for every method ?
It seems that sometimes I can just say (using namespace Foo) and not mention it again,
i.e. just define methods as
X::X() {…}
What is the correct approach here ?
Any of the three approaches you suggest will work. Given:
You can put the definition in a namespace block:
You can qualify the member name with the namespace name:
Or you can use a using directive (I’d not recommend this, though):
Generally, I’d recommend against using a using directive. As for the other two (using a namespace block or qualifying the names), I don’t think it really matters. I do both in my code.