Create
three
constint
values,
then add them together to produce a value that determines the size of an array
in an array definition. Try to compile the same code in C and see what happens
(you can generally force your C++ compiler to run as a C compiler by using a
command-line flag).
Prove
to yourself that the C and C++ compilers really do treat constants differently.
Create a global
const
and use it in a constant expression; then compile it under both C and C++.
Create
example
const
definitions for all the built-in types and their variants. Use these in
expressions with other
consts
to make new
const
definitions.
Make sure they compile successfully.
Create
a
const
definition in a header file, include that header file in two
.cpp
files, then compile those files and link them. You should not get any errors.
Now try the same experiment with C.
Create
a
const
whose value is determined at run time by reading the time when the program
starts (you’ll have to use the
<ctime>
standard header). Later in the program, try to read a second value of the time
into your
const
and see what happens.
Create
a
const
array of
char,
then try to change one of the
chars.
Create
an
extern
const
declaration in one file, and put a
main( )
in that file that prints the value of the
extern
const
.
Provide an
extern
const
definition in a second file, then compile and link the two files together.
Write
two pointers to
constlong
using
both forms of the declaration. Point one of them to an array of
long.
Demonstrate that you can increment or decrement the pointer, but you
can’t change what it points to.
Write
a
const
pointer to a
double,
and point it at an array of
double.
Show that you can change what the pointer points to, but you can’t
increment or decrement the pointer.
Write
a
const
pointer to a
const
object. Show that you can only read the value that the pointer points to, but
you can’t change the pointer or what it points to.
Remove
the comment on the error-generating line of code in
PointerAssignment.cpp
to see the error that your compiler generates.
Create
a character array literal with a pointer that points to the beginning of the
array. Now use the pointer to modify elements in the array. Does your compiler
report this as an error? Should it? If it doesn’t, why do you think that
is?
Create
a function that takes an argument by value as a
const;
then try to change that argument in the function body.
Create
a a function that takes a
float
by value. Inside the function, bind a
const
float&
to the argument, and only use the reference from then on to ensure that the
argument is not changed.
Modify
ConstReturnValues.cpp
removing comments on the error-causing lines one at a time, to see what error
messages your compiler generates.
Modify
ConstPointer.cpp
removing comments on the error-causing lines one at a time, to see what error
messages your compiler generates.
Make
a new version of
ConstPointer.cpp
called
ConstReference.cpp
which demonstrates references instead of pointers (you may need to look forward
to Chapter XX).
Modify
ConstTemporary.cpp
removing the comment on the error-causing line to see what error messages your
compiler generates.
Create
a class containing both a
const
and a non-
constfloat.
Initialize these using the constructor initializer list.
Create
a class called
MyString
which contains a
string
and has a constructor that initializes the
string,
and a
print( )
function. Modify
StringStack.cpp
so that the container holds
MyString
objects, and
main( )
so it prints them.
Create
a class containing a
const
member that you initialize in the constructor initializer list and an untagged
enumeration that you use to determine an array size.
In
ConstMember.cpp,
remove the
const
specifier on the member function definition, but leave it on the declaration,
to see what kind of compiler error message you get.
Create
a class with both
const
and non-
const
member functions. Create
const
and non-
const
objects of this class, and try calling the different types of member functions
for the different types of objects.
Create
a class with both
const
and non-
const
member functions. Try to call a non-
const
member function from a
const
member function to see what kind of compiler error message you get.
In
Mutable.cpp,
remove the comment on the error-causing line to see what sort of error message
your compiler produces.
Modify
Quoter.cpp
by making
quote( )
a
const
member function and
lastquotemutable.
Create
a class with a
volatile
data member. Create both
volatile
and non-
volatile
member functions that modify the
volatile
data member, and see what the compiler says. Create both
volatile
and non-
volatile
objects of your class and try calling both the
volatile
and non-
volatile
member functions to see what is successful and what kind of error messages the
compiler produces.
Create
a class called
bird
that can
fly( )
and a class
rock
that can’t. Create a
rock
object, take its address, and assign that to a
void*.
Now take the
void*,
assign it to a
bird*
(you’ll have to use a cast), and call
fly( )
through that pointer. Is it clear why C’s permission to openly assign via
a
void*
(without a cast) is a “hole” in the language, which couldn’t
be propagated into C++?