Link to home
Create AccountLog in
Avatar of ibanja
ibanja

asked on

What's the best way to not keep typing in long namespaces before functions and variables?

What is the best way to avoid the long namespaces such as lucene::index::IndexWriter?

Is it correct to just use the following:
using namespace lucene::index;
IndexWriter writer = new IndexWriter(indexDir, analyzer, true);

then change to another namespace:
using namespace lucene::analysis;
more code ...

using namespace std;
cout << "Hi";

Thanks,
ibanja
Avatar of Axter
Axter
Flag of United States of America image

Hi ibanja,
> Is it correct to just use the following:
> using namespace lucene::index;

Only in your *.cpp source file.
You should avoid doing this in the headers, and instead use fully qualified names.

David Maisonave (Axter)
Cheers!
ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
SOLUTION
Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
Avatar of ibanja
ibanja

ASKER

Thanks,

So if I understand correctly - multiple namespaces can be in use and:

using namespace lucene::index;
using namespace lucene::analysis;
using namespace std;
code ...
code ...
code ...

is the same as:

using namespace lucene::index;
code ...
using namespace lucene::analysis;
code ...
using namespace std;
code ...

since setting one name space doesn't nullify another namespace.
Yes, but it is common practice to put all of you "using" statements together at the top of the relevant file.
You can, but you are risking conflicts if these namespaces contain types that are named identically.
Avatar of ibanja

ASKER

Thanks,

I think I've got it.