namespace and global scope

W

Wat

If a class is not given a namespace, what does this imply?

From inside of the class, if to call global functions, should global scope
:: be used at all?

If global scope :: used, what benefits does it bring?

Thanks in advance!
 
V

Victor Bazarov

Wat said:
If a class is not given a namespace, what does this imply?

What do you mean by "given a namespace"? If it is declared outside of
any namespace, it is declared in the global namespace.
From inside of the class, if to call global functions, should global scope
:: be used at all?

Only to resolve name conflicts or for readability of the code.
If global scope :: used, what benefits does it bring?

The code becomes a bit more readable if nothing else.

V
 
B

Bob Hairgrove

What do you mean by "given a namespace"? If it is declared outside of
any namespace, it is declared in the global namespace.


Only to resolve name conflicts or for readability of the code.


The code becomes a bit more readable if nothing else.

It improves *maintainability*, but not necessarily just by improving
readability. It prevents name-lookup in other namespaces which might
not have been visible when the implementation was first written.

For example, I worked on a project once (in OS Windows) which involved
the open source library ungif. ungif (a library implemented in C
language, mostly in Unix/Linux operating systems) has a function
called "DrawText". C doesn't have namespaces. Of course, Windows API
(also written in C) also has a function called "DrawText". Hmmm ...
what did we do?

We changed the source of ungif so that if __cplusplus was defined, we
put "DrawText" into a namespace "ungif::DrawText". Worked out very
nicely...But what if we had tried to put the entire header into a
namespace?

That would also have been a possible solution. But then we have the
problem that someone might have placed a "using namespace ungif;"
somewhere in a file which also called the Win32API function
"DrawText".

You see where I am going to...?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top