3

Could somebody tell me what is the difference between

using namespace android;
    .... 

and

namespace android {
    ....
}

I found that almost all .cpp files in Android source code use the second one.
Also, If I want to include some files that use the type of second one in my own project, do I need to use namespace android{...} too?
Because if I do not, compiler would report error when I call methods of included files. Or do I need to add any prefix before method call?

iammilind
  • 68,093
  • 33
  • 169
  • 336
Bohan Lu
  • 513
  • 2
  • 6
  • 20
  • 2
    You need to read about how C++ namespaces work and what they are for. – Adrian Cornish Oct 19 '11 at 03:31
  • Sorry, I am new to C++, and I just know the "using namespace xxx". Thanks for your comment, I would study more about it. Thanks. – Bohan Lu Oct 19 '11 at 03:40
  • 1
    :-) cool Bohan Lu - all of use started somewhere - my advice is to learn as much as you can and ask a very specific question. Also iammilind wrote an excellent answer – Adrian Cornish Oct 19 '11 at 03:49
  • OK, I got it. Thanks for your answers and advice Adrian, iammilind and Gary. And I will pay more attention to ask question next time. Have a nice day. :-) – Bohan Lu Oct 19 '11 at 04:06

2 Answers2

6
namespace android {
  extern int i;  // declare here but define somewhere
  void foo ();
}

-- is used for scoping variables and functions inside a particular name. While using/calling those variables/functions, use scope resolution operator ::. e.g.

int main ()
{
  android::foo();
}

There is no restriction for putting all namespace declarations in a single body instance. Multiple namespace android bodies spread across several files, is possible and also recommended sometimes. e.g.

// x.cpp
namespace android {
  void somefunc_1 ();
}

// y.cpp
namespace android {
  void somefunc_2 ();
}

Now, sometimes you may find using :: operator inconvenient if used frequently, which makes the names unnecessarily longer. At that time using namespace directive can be used.

This using directive can be used in function scope / namespace scope / global scope; But not allowed in class scope: Why "using namespace X;" is not allowed inside class/struct level?).

int main ()
{
  using namespace android;
  foo(); // ok
}

void bar ()
{
  foo(); // error! 'foo' is not visible; must access as 'android::foo()'
}

BTW, Had using namespace android; declared globally (i.e. above main()), then foo() can be accessed without :: in Bar() also.

iammilind
  • 68,093
  • 33
  • 169
  • 336
  • Thank you, iammilind. I am really new to C++, thanks for your kind and detailed explanation. I would study more about it, thanks again. – Bohan Lu Oct 19 '11 at 03:48
2

My answer is probably only helpful if you are more experienced with Java. I'm guessing since you are doing android stuff that this is the case.

The following means that you are declaring a class called MyClass in the namespace android. The qualified name of the class would be android::MyClass.

namespace android {
    class MyClass {...};
}

It can be thought of similarly to the Java code:

package android;

public class MyClass {...}

The following means that you can use classes, functions etc. defined in the android namespace without having to use their qualified name (assuming they have been included).

using namespace android;

This

#include <path/to/MyClass.h>
using namespace android;

can be thought of similarly to the Java code:

import android.MyClass;
Gyan aka Gary Buyn
  • 12,242
  • 2
  • 23
  • 26
  • Thank you, Gary. Yes, I am doing the Android stuff. I am just familiar with C and Java not C++. Thanks for your explanation so I now understand the difference. Thank you. – Bohan Lu Oct 19 '11 at 03:55