Namespace
Contents
q
The Namespace Definition
q
Explicit Namespace
Qualification
q
The using
Declaration
q
The using
Directive
q
The Namespace Alias
q
The Global namespace
q
unnamed Namespace
Definitions
q
The std
Namespace
The namespace Definition
You define a namespace by surrounding all the associated code with a
namespace block.
namespace
MumbleFratz {
//Everything within these braces is in namespace mumblefratz
}
Back to top
Explicit namespace Qualification
To qualify an identifier explicitly with its namespace, you use the ::
scope resolution operator.
E.g.,
MyLibrary :: counter++
Back to top
The using Declaration
A using declaration specifies that all the identifiers in the namespace are
available to the program within the scope of the using declaration.
#include
"soandso"
using namespace MumbleFratz;
Back to top
The using Directive
The using directive tells the compiler you intend to use specific
identifiers within a namespace. The using directive places those specific
identifiers in the directive's scope, as if they had been declared where the
directive appears.
using MumbleFratz::Desktop;
Back to top
The Namespace Alias
The namespace feature allows a program to assign an alias to a namespace
identifier, as shown here:
namespace MF = MumbleFratz;
Back to top
The Global namespace
Any non-static external identifier declared in global space outside any
namespace definition is said to be in the global namespace. You may still reference global identifiers
just as you could in the past. If the identifiers are the same as other identifiers
in other namespaces, you can reference them by using the :: scope resolution
operator.
Back to top
The std Namespace:
All identifiers declared as part of the public interface of the Standard
C++ library are declared in the std
namespace.
Back to top
Unnamed namespace Definitions
An unnamed namespace
definition omits the namespace identifier. The compiler generates an internal
identifier that is unique throughout the program. Identifiers declared within
an unnamed namespace definition are available only within the translation unit
within which they are declared.
Back to top
Variables
Intrinsic Data Types Size (bits) –
depends on the compiler
bool
|
|
true or false, converts to int 1 or
0, numeric non-zero converts to true.
|
char
|
8
|
signed or unsigned
|
wchar_t
|
|
Wide character type, supporting international character
sets.
|
int
|
16(short) 32(int & long)
|
signed (default), unsigned, long, or short [int is
optional for unsigned, long or short]
|
float
|
64
|
Range of precision for float, double and long double
depend on the implementation. See the <cfloat> header file for the
particular compiler
|
double
|
64
|
Also long double.
|
Constants (literal constants as opposed to use of the const
keyword)
Character Constants
‘A’ (literal),
‘\x2f’ (hex), ‘\014’ (octal)
Integer Constants
-129 (decimal integer),
0x12fe (hexadecimal integer),
0177 (octal integer) [that’s a leading zero]
52388L (long integer),
0x4feaL (long hex), 40000U
(unsigned int)
Floating Constants
3.14159 (regular),
1.2E-40 (1.2 x 10 raised to the -40th power), 2.4E201 (2.4 x 10 to the 201st
power)
Floating constants will default to double unless you use the
F suffix (float) or the L suffix (long double)
Escape Sequences
\a
|
audible bell character
|
\b
|
backspace
|
\f
|
form feed
|
\n
|
newline
|
\r
|
carriage return
|
\t
|
horizontal tab
|
\v
|
vertical tab
|
\\
|
backslash
|
\0
|
null (zero) character
|
\”
|
double quote
|
\0nnn
|
octal number
|
\xhh
|
hex number
|
String Constants (literals – not objects of type std::string)
“Hello World!”,
“\nNewline and text”, “\aBell
and text”
Back to top
Console Input and Output
Manipulators
The three manipulators are std::hex,
std::dec, and std::oct.
The Standard Streams
The standard streams are std::cout,
std::cin, and std::cerr
Back to top
Enumerations
An enumerator declaration defines a data type that encapsulates a sequence
of enumerated integer constants, as
follows:
enum Colors (red, green, blue); //where the first identifier
in the list will be integer 0, the second will be integer 1, etc., by default.
The first integer can be assigned any integer value (e.g., red=12) and the
rest of the list will be incremented by 1.
Back to top
The main Function and its Arguments
The main function has two arguments and should be declared as follows:
int main ( int argc,
char* argv[ ] )
{
//…
return;
}
Argc contains the number of command line arguments. Argv[ ] is an array of
char pointers, the first of which, argv[0], points to the name of the program
being executed.
If the program returns a zero or does not return any value, it indicates
success, and a nonzero returned value means unsuccessful.
Back to top
Name Mangling and
Linkage-Specifications
Be advised that C++ compilers mangle the names of the functions, but they
do not do it in consistent ways from compiler to compiler. Therefore, any
linking of code that was compiled by different compilers or by different
languages is likely to cause link-time errors unless linkage-specifications are
used. One of the reasons for name mangling is in support of type-safe linkage.
If you have a library of custom C
functions to include in your C++ system and you do not want to port them to C++, then you must use a
linkage-specification.
To tell the C++ compiler that the functions in a header file, mychdr.h,
were compiled by a C compiler:
extern “C” {
#include
“mychdr.h”
}
To define C++ functions that C programs are going to call, use this
convention in the C++ program:
extern “C” {
void
myfunc(int x, y )
{
….
}
}
See Stevens, page 183, for examples.
Back to top
The Preprocessor
Preprocessing Directives and Including Files
The # precedes all preprocessing directives.
#
|
Null directive – no action
|
#include
|
Include a source code file (see note below)
|
#define
|
Define a macro
|
#undef
|
Remove the definition of a macro
|
#if
|
Compile code if the condition is true
|
#ifdef
|
Compile code if macro is defined
|
#ifndef
|
Compile code if macro is not defined
|
#else
|
Compile code if previous if condition is not true
|
#elif
|
Compile code if previous if condition is not true and
current condition is true
|
#endif
|
Terminate #if …
#else conditional block
|
#error
|
Stop compilation and displays error message
|
|
|
The #include preprocessing directive includes the named file
in the translation unit,
replacing the directive.
<…> means to search from among
the header files that are part of the compiler system.
“…..” means to search from among
the files in the project directory or folder. If not found there, then the
search continues in the compiler system.
Macros: #define and #undef
#define identifier
expression
……
#undef identifier
Defining a macro tells the compiler to substitute the expression for the
identifier wherever it is found in the code subsequent to the definition and
prior to an #undef of that macro. An extra set of parens around the expression
can often prevent an unexpected result as a result of precedences.
Stringizing and Concatenation
Stringizing ( # ) - When appearing
in a macro, the # symbol is a
Stringizing operator. It converts its argument to a string and concatenates
that string to whatever string precedes it, as shown in this example:
#define Error(n)
std::cout “Error “ << n
can be written instead as:
#define Error(n)
std::cout “Error “ #n
Concatenation ( ## ) - When appearing in a macro, the ## symbol is a concatenation
operator. It takes the two parameters on either side and concatenates them into
a single token. Note that it does not first evaluate variables – it just
concatenates whatever was passed as an argument.
Compile Time Conditionals
Using the preprocessor’s compile time conditionals allows the compiler to
either compile or ignore entire blocks of code according to the value of an
expression or whether or not a given macro identifier is defined. In addition
to the conditionals in the table above, the expressions #if defined and #if
!defined perform the same as #ifdef and #ifndef
Other Standard Directives and Definitions
#line directive enables you to change the file name and line number the compiler
uses to report subsequent warning and error messages like this:
#line 999 “foobar.cpp” //Note that Stevens fails to see the usefulness of this
macro.
The following table has the Standard Defined Macro Names. They enable the
programmer to emit error messages with the std
:: assert macro that specify on the console where in the source code
file certain errors have been found.
__LINE__
|
The line number of the current source code line
|
__FILE__
|
The name of the current source code fine
|
__DATE__
|
The date the source code was compiled (“mmm dd yy”)
|
__TIME__
|
The time the source code was compiled (“hhmmss”)
|
__STDC__
|
Identified but not defined by the standard
|
__cplusplus
|
Defined as the const long integer value 199711L when the
source code being compiled is C++ code but not defined otherwise.
|
__STDC__
|
Is typically defined to specify that a program should be
compiled by using Standard C++ conventions only and no compiler-specific
language extensions.
|
__cplusplus
|
Is typically used in header files that can be shared
between C and C++ development environments and need to provide different
source code for the two language processors.
|
|
|