Use of "using namespace" when declaring that namespace - c++

Suppose I have the following files:
// SomeClass.h
namespace Example
{
class SomeClass
{
...
SomeClass someFunction();
...
};
}
// SomeClass.cpp
Example::SomeClass Example::SomeClass::SomeFunction()
{
...
}
Would there be any consequences to add "using namespace Example;" before the namespace in SomeClass.h to eliminate the need of adding the "Example::" scope operator to things in the Someclass.cpp file? Even if there are no conesequences, would this be considered bad coding practice?
The change would be as follows:
// SomeClass.h
using namespace Example;
namespace Example
{
class SomeClass
{
...
SomeClass someFunction();
...
};
}
// SomeClass.cpp
SomeClass SomeClass::SomeFunction()
{
...
}

No, please don't put using namespace ...; in the global area. You can just do this:
SomeClass.h
// using namespace Example; // never here please
namespace Example
{
using namespace OtherExample; // this is okay (not global)
class SomeClass
{
...
SomeClass someFunction();
...
};
}
SomeClass.cpp
namespace Example // same as in .h
{
using namespace OtherExample; // this is okay (not global)
SomeClass SomeClass::SomeFunction()
{
...
}
}
And I would also suggest with potentially huge namespaces like std:: to never use using namespace std; even within your own namespaces because they simply drag in too many common symbol names.

Related

namespace question in c++ how to use "using namespace"

I have a very basic question about namespace
when should I use "using namespace A::B"?
I know I shouldn't use it in header files, how about .cpp file? In my test.cpp:
namespace A{
namespace B{
namespace C{
A::B::Object obj = ....
}
}
}
the full namespace is A::B::Object as I have right now, Do I actually need A::B? Can I just have Object obj = ....? Should I use using namespace A::B and then have Object obj = ....? I am not sure what this actually means:
namespace A{
namespace B{
namespace C{
.....
}
}
}
I know This means all the contents inside it will have namespace A::B::C, but does it also mean:
using namespace A
using namespace A::B
using namespace A::B::C
implicitly for the contents inside it?
Because obj is not on the root (global) namespace if you write outside its namespace Object the identifier will be not found. So you have a few options:
use fully qualified name:
A::B::C::Object obj = ...
use using declaration:
using namespace A::B::C;
Object obj = ...;
use using declaration:
using A::B::C::Object
Object obj = ...
use namespace alias:
namespace X = A::B::C;
X::Object obj = ...
And basically any combination of above e.g.:
namespace Y = A::B;
Y::C Object obj = ...
using namespace A::B;
C::Object obj = ...
Statements placed in the inner namespaces will be able to reference classes, typedefs, functions, etc from the outer namespaces without explicitly typing them.
namespace A {
class AClass { };
namespace B {
class BClass {
AClass a; // Does not require A::AClass
};
}
}
In addition to using namespace, another way to shorten lengthy compounded namespaces in a .cpp file is to declare a new, shorter namespace:
// MyClass.h
namespace A {
namespace B {
class MyClass {
public:
void f();
};
}
}
// MyClass.cpp
#include "MyClass.h"
namespace AB = ::A::B;
void AB::MyClass::f() {
// impl...
}
(Note that the optional :: at the start of ::A::B explictly tells the compiler that ::A::B is the full path, which is potentially useful if a header had an SomeOtherSpace::A::B namespace and carelessly wrote using namespace SomeOtherSpace.)

Is it possible to use class name which declare in namespace without namespace::

Is it possible to use class name which declare in namespace without namespace?
I tried like this:
//MyClass.h
namespace MyNamespace {
class MyClass;
}
class MyNamespace::MyClass {
public:
/* ... */
}
//MyOtherClass.cpp
#include "MyClass.h"
using namespace MyNamespace;
void MyOtherClass::MyFunction() {
MyClass *myClass = new MyClass;
}
But it doesn't work.
Complier says "MyClass is ambiguous."
I guess this code would work :
//MyOtherClass.cpp
#include "MyClass.h"
using namespace MyNamespace;
void MyOtherClass::MyFunction() {
MyNamespace::MyClass *myClass = new MyClass;
}
But it is uncomfortable for me.
I wanna use 'MyClass' without "MyNamespace::".
Is it possible?
Thanks for your help.
I got answer : It is not possible.
C++ classes have own default namespace. If I declare MyNamespace, then there is two namespaces associated with MyClass. If I use MyClass without choosing what namespace I want to use, Compiler feel ambiguous what namespace I really wanted to use. So it would tell me "MyClassis ambiguous."
if the below code is all in the same .h file (you did not really clarify)
//MyClass.h
namespace MyNamespace {
class MyClass;
}
class MyNamespace::MyClass {
public:
/* ... */
}
Then what you are doing a forward declaration of MyNamespace::MyClass in the first part.
You should have something like this in MyClass.h
namespace MyNamespace {
class MyClass
{
public:
// constructor, methods,...
}; // end of class declaration
} // end of namespace
and the associated definition in MyClass.cpp

Hiding declaration in cpp file

I have a file decl.h with the following:
namespace foo {
...
class A;
...
}
I want to use the whole of declarations from decl.h, except for class A, as I want to have another class, with the same name, declared and defined inside my def.cpp. I'm looking for something that'd allow me to do this:
# include "decl.h"
using namespace foo;
hiding foo::A;
class A {
...
};
Is there anything like that? Or the only way around is to explicitly make each desired member from foo public in my def.cpp?
Just remove using namespace foo;. That's the whole point of namespaces.
You can't hide members of a namespace, and certainly not when using a using namespace ... statement.
The whole point of namespaces is to avoid naming conflicts like you describe.
So, get rid of the using namespace foo; statement, and wrap the second class A in a different namespace, eg:
#include "decl.h"
//using namespace foo;
namespace defcpp {
class A {
...
};
}
Now def.cpp will know about foo::A and defcpp::A. You just have to qualify which one you want to use whenever you need to use an A. For example:
#include "decl.h"
//using namespace foo;
namespace defcpp {
class A {
...
};
}
class B {
defcpp::A a;
...
};
void doSomething()
{
defcpp::A a;
...
}

anonymous namespace in c++ cannot access header

I have an enum in the header:
namespace somespace
{
namespace internal
{
class SomeClass
{
public:
typedef enum
{
kNone = 0,
kKaka = 1,
}SomeEnum;
}
}
}
In the cpp, we sometimes use an anonymous namespace with the helper functions.
#include <somespace/internal/SomeClass.h>
using somespace::internal;
namespace
{
bool helpMe(SomeEnum& foo) //does not recognize the enum in the header
{
}
}
void SomeClass::memberMethod
{
}
But I cannot access the SomeEnum in the .cpp file. Why is that?
How can I get around this without polluting the internal namespace for example?
SomeEnum is scoped to the class name it is declared in. To use it you need SomeClass::SomeEnum. This assumes that SomeClass is accessible in the scope you have it. If not then you need somespace::internal::SomeClass::SomeEnum

C++ namespace issue

I am having three classes all of them are from different namespaces as shown below:
classA.h
namespace outer
{
namespace inner
{
class ClassA
{
....
};
}
}
classB.h
namespace inner
{
class ClassB
{
...
};
}
classC.h
#include <classB.h>
namespace outer
{
namespace inner2
{
using inner::ClassB; // error here, says outer::inner2::ClassB has not been declared.
class ClassC
{
....
};
}
}
I am stuck at this please help me to solve this issue.
You need
using ::inner::ClassB;
because in namespace outer, you have 2 options for inner
::inner - global namespace
::outer::inner - outer namespace
By default, using inner::ClassB; will try to import ClassB from outer::inner.