Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 379
  • Last Modified:

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
0
ibanja
Asked:
ibanja
  • 2
  • 2
  • 2
  • +1
2 Solutions
 
AxterCommented:
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!
0
 
jkrCommented:
In order to avoid namespace conflichts, you could narrow that down to

using lucene::index::IndexWriter;
IndexWriter writer = new IndexWriter(indexDir, analyzer, true);

and not introduce the whole namespace at all.
0
 
Carl TawnSystems and Integration DeveloperCommented:
"then change to another namespace:" <-- You can use multiple "using" statements in the same file.

i.e.

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

   IndexWriter writer = new IndexWriter(indexDir, analyzer, true);

Although purests would frown on just using "namespace std;" and would suggest that you should explictly declare which members of the namespace you are using:

i.e.

    using std::cout;
    using std::cin;
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
ibanjaAuthor Commented:
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.
0
 
Carl TawnSystems and Integration DeveloperCommented:
Yes, but it is common practice to put all of you "using" statements together at the top of the relevant file.
0
 
jkrCommented:
You can, but you are risking conflicts if these namespaces contain types that are named identically.
0
 
ibanjaAuthor Commented:
Thanks,

I think I've got it.
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 2
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now