My Notes C++ Study 02

You might also like

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 2

In C++, pointers are declared using the * symbol and you access a memory address

with the
& operator:
int *p;
int i = 42;
p = &i;
The first line declares a variable, p, which will be used to hold the memory
address of an
integer. The second line declares an integer and assigns it a value. The third line
assigns a
value to the pointer p to be the address of the integer variable just declared. It
is important
to stress that the value of p is not 42; it will be a memory address where the
value of 42 is
stored.
Note how the declaration has the * on the variable name. This is common convention.
The
reason is that if you declare several variables in one statement, the * applies
only to the
immediate variable. So, for example:
int* p1, p2;
Initially this looks like you are declaring two integer pointers. However, this
line does not
do this; it declares just one pointer to integer called p1. The second variable is
an integer
called p2. The preceding line is equivalent to the following:
int *p1;
int p2;
If you wish to declare two integers in one statement, then you should do it as
follows:
int *p1, *p2;
Understanding Language Features
[ 61 ]
Using namespaces
Namespaces give you one mechanism to modularize code. A namespace allows you to
label
your types, functions, and variables with a unique name so that, using the scope
resolution
operator, you can give a fully qualified name. The advantage is that you know
exactly which
item will be called. The disadvantage is that using a fully qualified name you are
in effect
switching off C++'s argument-dependent lookup mechanism for overloaded functions
where
the compiler will choose the function that has the best fit according to the
arguments passed
to the function.
Defining a namespace is simple: you decorate the types, functions, and global
variables
with the namespace keyword and the name you give to it. In the following example,
two
functions are defined in the utilities namespace:
namespace utilities
{
bool poll_data()
{
// code that returns a bool
}
int get_data()
{
// code that returns an integer
}
}
Do not use semicolon after the closing bracket.
Now when you use these symbols, you need to qualify the name with the namespace:
if (utilities::poll_data())
{
int i = utilities::get_data();
// use i here...
}
The namespace declaration may just declare the functions, in which case the actual
functions would have to be defined elsewhere, and you will need to use a qualified
name:
namespace utilities
{
// declare the functions
bool poll_data();
Understanding Language Features
[ 62 ]
int get_data();
}
//define the functions
bool utilities::poll_data()
{
// code that returns a bool
}
int utilities::get_data()
{
// code that returns an integer
}
One use of namespaces is to version your code. The first version of your code may
have a
side-effect that is not in your functional specification and is technically a bug,
but some
callers will use it and depend on it. When you update your code to fix the bug, you
may
decide to allow your callers the option to use the old version so that their code
does not
break. You can do this with a namespace:
namespace utilities
{
bool poll_data();
int get_data();
namespace V2
{
bool poll_data

You might also like