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?

4
  • 2
    You need to read about how C++ namespaces work and what they are for. Commented Oct 19, 2011 at 3: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. Commented Oct 19, 2011 at 3: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 Commented Oct 19, 2011 at 3: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. :-) Commented Oct 19, 2011 at 4:06

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, iammilind. I am really new to C++, thanks for your kind and detailed explanation. I would study more about it, thanks again.
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;

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.