Sams Teach Yourself C++ in 21 Days

(singke) #1
LISTING18.6B Grouping Related Items
0: // header2.h
1: namespace Window
2: {
3: void resize( int x, int y ) ;
4: }

As you can see, the Windownamespace is spread across both header files. The
compiler treats both the move()function and the resize()function as part of the
Windownamespace.

Declaring and Defining Types ........................................................................


You can declare and define types and functions within namespaces. Of course, this is a
design and maintenance issue. Good design dictates that you should separate interface
from implementation. You should follow this principle not only with classes but also with
namespaces. The following example demonstrates a cluttered and poorly defined name-
space:
namespace Window {
//... other declarations and variable definitions.
void move( int x, int y) ; // declarations
void resize( int x, int y ) ;
//... other declarations and variable definitions.

void move( int x, int y )
{
if( x < MAX_SCREEN_X && x > 0 )
if( y < MAX_SCREEN_Y && y > 0 )
platform.move( x, y ) ; // specific routine
}

void resize( int x, int y )
{
if( x < MAX_SIZE_X && x > 0 )
if( y < MAX_SIZE_Y && y > 0 )
platform.resize( x, y ) ; // specific routine
}
//... definitions continue
}
You can see how quickly the namespace becomes cluttered! The previous example is
approximately 20 lines in length; imagine if this namespace were four times longer.

644 Day 18


ANALYSIS
Free download pdf