0

Following code is not legal:

// .cpp file

{
  using namespace X;
  char text[] = {someENUMinX, someOtherENUMinX, ... };
}

The {} block gives error when used that way. How to accomplish this? I.e. to have ability to use whole namespace for some portion of variable declarations.

To put it in other way: is it possible to revoke using namespace? I have like 40 members in each namespace, and would like to switch to them during defining variables.

PS.: I am sorry for multiple edits. The question can be deleted if it's a problem.

AllCoder
  • 327
  • 1
  • 2
  • 8
  • This question has gone down the drain completely. Follow the Unix philosophy here: Ask one thing and do it correctly. Changing the question multiple times is a clear sign of blatant disrespect and totally un-values the answers given. I vote for the question being closed, no other kind soul should waste his time here. – Sebastian Mach Aug 23 '12 at 11:34

2 Answers2

2

This is convenient, because in .cpp I can use namespace X during defining vars – the X::someXcall() – and the var is then accessible in global namespace,

This is just wrong. It is not accessible in the global namespace. The extern line is not a declaration of X::text. What extern char text[]; does is declare a different variable named text in the global namespace.

And more importantly, why would having that in the global namespace be desirable? Why would anyone want to use namespaces and yet defeat their entire purpose at the first opportunity?

R. Martinho Fernandes
  • 228,013
  • 71
  • 433
  • 510
  • I gave you the answer on last question: defining variable is not the same as using it. Two different actions – in first you may want to USE a namespace member – but that should not influence the using part. PS. Size of the array is not needed, though I am giving it. – AllCoder Aug 23 '12 at 10:21
  • @AllCoder I have no idea what is this "last question" you're talking about. – R. Martinho Fernandes Aug 23 '12 at 10:22
  • The question: "Why would anyone want to use namespaces..." – AllCoder Aug 23 '12 at 10:24
  • If `extern char text[];` were indeed a declaration of `X::text`, the variable would effectively be in the global namespace, because it would prevent you from declaration another one anywhere. To bring stuff into another scope you use `using`, not declarations. – R. Martinho Fernandes Aug 23 '12 at 10:29
  • Is it possible to revoke using namespace? I have like 40 members in each namespace, and would like to switch it during defining variables. – AllCoder Aug 23 '12 at 10:31
0

Your program has 2 distinct objects named text.

extern char text[] declared in .h file resides in the global namespace. Is it not the same as char text[] in namespace X.

Andriy
  • 8,486
  • 3
  • 27
  • 51