C++ & Design Patterns - Linked List, Namespaces

A Linked List Design:

First we'll separate it from any other code using namespaces (just like we would with packages). Stroustrup p.165-185

Basic syntax:


namespace foo
{

} 

This is used in headers. In source files for implementation we use scope resolution to refer to things in a namespace or when implementing sometimes. One can use using declarations like imports to bring in individual names or all names in a namespace. This can be done basically anywhere not just in specific places. Either using foo::Bar; or using namespace bar, Stroustrup recommends avoiding the second except inside functions. They can also be used to mix in a namespace to another. Note that namespaces are open an can be added to allowing us to specify multiple interfaces to a namespace, one for users and the other for implementors. If a function is not defined in the current namespace we search in the namespace of the arguments.

An unnamed namespace can be used too hide things from outside files. They have implied using directives after them.

Namespaces can be given aliases by namespace foo=longer_foo; Also we can make up a namespace containing only the items we want from someone else's namespace.

For historical reasons, overloading works across across namespaces.

Abstract List:

Describe the abstract concept with an abstract class. Note the use of the virtual keyword and of pure virtual to mean abstract. We need to make a virtual destructor too. Finally lets make a destroy method because for those of you thinking ahead we need to be able to delete nodes without deleting the Singleton null node.



jwalker@cs.oberlin.edu