Namespace and Using Flashcards
What is a Namespace?
A place to store and provide scope to variables, functions, classes, etc. to provide obfuscation and avoid namespace collision.
Namespace and Using
How do you create a namespace?
namespace namespace_name {
…Code in namespace…
}
Namespace and Using
How do you reference a namespace?
- directly by name (namespace_name::var/function/class)
- via the ‘using’ keyword. (using namespace_name)
Namespace and Using
If two files are imported with the same namespace, what happens?
The namespaces are merged into a single namespace.
Namespace and Using
What is the default namespace?
The Global namespace
Namespace and Using
What is the benefit of the global namespace?
Things in the global namespace can be accessed without ‘using’ or ‘::’
What does the ‘using namsepace’ keyword do in a .cpp file?
It brings the mentioned namespace into the global namespace.
What does the ‘using namsepace’ keyword do in a .h file?
It pulls the headerfile’s material in the mentioned space.
Why should the ‘using’ keyword be avoided in .h files?
- It can create subnamespaces if namespaces are created in the .h file.
- It pulls the .h file into the “using” namespace rather than the global namespace.
Namespace and Using
What is a subnamespace?
A namespace that can only be accessed from the namespace it was declared in.
Namespace and Using
What does a directive allow you to do?
Specify single elements from a namespace that you can include in your global namespace.
How do you declare a directive?
using namespace::element;
ex: using std::cout;
What does the error “____ is ambiguous” signify?
That the namespace of an object / method needs to be specified as there is namespace collision.
What is the ‘friend’ keyword?
It Grants access to viewing and interacting with the private member variables / methods of the class.
What are the rules to the ‘friend’ keyword?
- Only offered, not taken.
- Not bidirectional.
- Friendship is not inherited.
- Inheritence is not friendship.