We're always interested in getting feedback. E-mail us if you like
this guide, if you think that important material is omitted, if you
encounter errors in the code examples or in the documentation, if you
find any typos, or generally just if you feel like e-mailing. Mail to
Frank Brokken
or use an
e-mail form.
Please state the concerned document version, found in
the title. If you're interested in a printable
PostScript copy, use the
form, or
better yet,
pick up your own copy by ftp
from ftp.icce.rug.nl/pub/http.
int
s. Indexing the array elements occurs
with the standard array operator []
, but additionally the class checks
for boundary overflow. Furthermore, the array operator is interesting in that
it both produces a value and accepts a value, when used, respectively,
as a right-hand value and a left-hand value in expressions.
An example of the use of the class is given here:
int
s. The elements
of the array can be assigned or retrieved. The above example should produce a
run-time error, generated by the class IntArray
: the last
for
loop causing a boundary overflow, since x[20]
is addressed while
legal indices range from 0 to 19, inclusive.
We give the following class interface:
int
argument,
specifying the array size. This function serves also as the default
constructor, since the compiler will substitute 1 for the argument when
none is given.
IntArray
object.
This overloaded operator has for its prototype a function
returns a reference to
an int
. This allows us to use expressions like x[10]
on the left-hand side and on the right-hand side of an assignment.
We can
therefore use the same function to retrieve and to assign values.
Furthermore note that the returnvalue of the overloaded array operator is
not a int const &
, but rather a int &
. In this situation we
don't want the const
, as we must be able to change the element
we want to access, if the operator is used as a left-hand value in an
assignment.
destroy()
, as this function would
consist of merely one statement (delete data
).
data
are int
s, no delete []
is needed.
It doesn't do no harm, either. Therefore, since we use the []
when the
object is created, we also use the []
when the data are eventually
destroyed.
The member functions of the class are presented next.
operator new
is overloaded, it must have a void *
return type,
and at least an argument of type size_t
. The size_t
type is defined in
stddef.h
), which must therefore be included when the operator new
is overloaded.
It is also possible to define multiple versions of the operator new
, as
long as each version has its own unique set of arguments. The global new
operator can still be used, through the ::
-operator. If a class X
overloads the operator new
, then the system-provided operator new
is
activated by
X *x = ::new X();
Furthermore, the new []
construction will always use the default operator
new
.
An example of the overloaded operator new
for the class X
is the
following:
Now, what happens when the operator new
is defined for the class X
,
assuming that class is defined as follows (For the sake of simplicity
we have violated the principle of encapsulation here. The principle of
encapsulation, however, is immaterial to the discussion of the workings of
the operator new
.):
Next, consider the following program fragment:
This small program produces the following output:
0, 0, 0
Our little program performed the following actions:
X
object.
X()
constructor. Since no constructor was defined,
the constructor itself didn't do anything at all.
new
operator
the allocated X
object was already initialized to zeros when the
constructor was called.
Non-static object member functions are passed a (hidden) pointer to the object
on which they should operate. This hidden pointer becomes the this
pointer
inside the memberfunction. This procedure is also followed by the constructor.
In the following fragments of pseudo C++
the pointer is made visible. In the first
part an X
object is declared directly, in the second part of the example
the (overloaded) operator new
is used:
C++
fragment the member functions were treated
as static functions of the class X
. Actually, the operator new()
operators is a static functions of its class: it cannot reach data members
of its object, since it's normally the task of the operator new()
to create
room for that object first. It can do that by allocating enough memory, and
by initializing the area as required. Next, the memory is passed over to the
constructor (as the this
pointer) for further processing. The fact that
an overloaded operator new
is in fact a static function, not requiring
an object of its class can be illustrated in the following (discouraged
in normal situations !) program fragment, which can be compiled without
problems (assume class X
has been defined and is available as before):
X::operator new()
returns a void *
to an initialized block
of memory, the size of an X
object.
The operator new
can have multiple parameters. The first parameter again
is the size_t
parameter, other parameters must be passed during the
call to the operator new
. For example:
X
object for which the memory has
been allocated by the call to the first overloaded operator new
, followed
by the call of the constructor X()
for that block of memory.
The object (object2) is a pointer to an X
object for which the memory has
been allocated by the call to the second overloaded operator new
, followed
again by a call of the constructor X()
for its block of memory.
Notice that object3
also uses the second overloaded operator new()
:
that overloaded operator accepts a variable number of arguments, the first
of which is a char const *
.
delete
operator may be overloaded too. The operator delete
must
have a void *
argument, and an optional second argument of type size_t
,
which is the size in bytes of objects of the class for which the operator
delete
is overloaded. The returntype of the overloaded operator delete
is
void.
Therefore, in a class the operator delete
may be overloaded using the
following prototype:
void operator delete(void *);
or
void operator delete(void *, size_t);
The `home-made' delete
operator is called after executing the class'
destructor. So, the statement
delete ptr;
with ptr
being a pointer to an object of the class X
for which the
operator delete
was overloaded, boils down to the following statements:
The overloaded operator delete
may do whatever it wants to do with the
memory pointed to by ptr
. It could, e.g., simply delete it. If that
would be the preferred thing to do, then the default delete
operator
can be activated using the ::
scope resolution operator. For example:
C++
streams cout
and cerr
and the insertion operator
<<
. Adaptation of a class for the usage with cin
and
its extraction operator >>
occurs in a similar way and is not illustrated
here.
The implementation of an overloaded operator <<
in the context
of cout
or cerr
involves the base class of cout
or
cerr
, which is ostream
. This class is declared in the header
file iostream.h
and defines only overloaded operator functions for
`basic' types, such as, int
, char*
, etc.. The purpose of
this section is to show how an operator function can be defined which
processes a new class, say Person
(see section 5) ,
so that constructions as the following one become possible:
The statement cout << kr
involves the operator <<
and its two operands: an ostream&
and a Person&
. The
proposed action is defined in a class-less operator function
operator<<()
expecting two arguments:
Concerning this function we remark the following:
ostream
object,
to enable `chaining' of the operator.
<<
are stated as
the two arguments of the overloading function.
ostream
provides the member function
opfx()
, which flushes any other ostream
streams tied
with the current stream. opfx()
returns 0 when an error has been
encountered.
An improved form of the above function would therefore be:
String
around the char *
. Such a class may define all
kinds of operations, like assignments. Take a look at the following
class interface:
char const *
, and
also from a String
itself. There is an overloaded assignment operator,
allowing the assignment from a String
object and from a
char const *
(Note that the assingment from a char const *
also includes the null-pointer. An assignment like stringObject = 0
is
perfectly in order.).
Usually, in classes that are less directly linked to their data than this
String
class, there will be an accessor member function, like
char const *String::getstr() const
. However, in the current context that
looks a bit awkward, but it also doesn't seem to be the right way to
go when an array of strings is defined, e.g., in a class StringArray
,
in which the operator[]
is implemented to allow the access of individual
strings. Take a look at the following class interface:
The StringArray
class has one interesting memberfunction: the overloaded
array operator operator[]
. It returns a String
reference.
Using this operator assignments between the String
elements can be
realized:
It is also possible to assign a char const *
to an element of sa
:
sa[3] = "hello world";
sa[3]
is evaluated. This results in a String
reference.
String
class is inspected for an overloaded assignment,
expecting a char const *
to its right-hand side. This operator is
found, and the string object sa[3]
can receive its new value.
Now we try to do it the other way around: how to access the
char const *
that's stored in sa[3]
? We try the following code:
String
to a char const *
.
How to proceed?
The naive solution is to resort to the accessor member function getstr()
:
cp = sa[3].getstr();
A conversion operator is a kind of overloaded operator, but this time the
overloading is used to cast the object to another type. Using a conversion
operator a String
object may be interpreted as a char const *
, which
can then be assigned to another char const *
. Conversion operators can be
implemented for all types for which a conversion is needed.
In the current example, the class String
would need a conversion operator
for a char const *
. The general form of a conversion operator in the class
interface is:
operator <type>();
String
class, it would therefore be:
operator char const *();
The implementation of the conversion operator is straightforward:
Notes:
operator
keyword.
printf("%s", sa[3]);
String &
or a
char const *
to the printf()
function? To help the compiler
out, we supply an explicit cast here:
printf("%s", (char const *)sa[3]);
For completion, the final String
class interface, containing the
conversion operator, looks like this:
However, some of these operators may only be overloaded as member functions
within a class. This holds true for the '='
, the '[]'
, the
'()'
and the '->'
operators. Consequently, it isn't possible
to redefine, e.g., the assignment operator globally in such a way that
it accepts a char const *
as an lvalue
and a String &
as an
rvalue. Fortunately, that isn't necessary, as we have seen in section
6.4.