330 Thinking in C++ www.BruceEckel.com
class X { void f(); };
the function
f( )
inside the scope of
class X
does not clash with the
global version of
f( )
. The compiler performs this scoping by
manufacturing different internal names for the global version of
f( )
and
X::f( )
. In Chapter 4, it was suggested that the names are simply
the class name “decorated” together with the function name, so the
internal names the compiler uses might be
_f
and
_X_f
. However, it
turns out that function name decoration involves more than the
class name.
Here’s why. Suppose you want to overload two function names
void print(char);
void print(float);
It doesn’t matter whether they are both inside a class or at the
global scope. The compiler can’t generate unique internal
decorate the return value with the internal function name. Then
you could overload on return values, as well:
void f();
int f();
This works fine when the compiler can unequivocally determine
the meaning from the context, as in
int x = f( );
. However, in C
you’ve always been able to call a function and ignore the return
value (that is, you can call the function for its
side effects
). How can
the compiler distinguish which call is meant in this case? Possibly
worse is the difficulty the reader has in knowing which function
call is meant. Overloading solely on return value is a bit too subtle,
and thus isn’t allowed in C++.
Type-safe linkage
There is an added benefit to all of this name decoration. A
particularly sticky problem in C occurs when the client
programmer misdeclares a function, or, worse, a function is called
without declaring it first, and the compiler infers the function
declaration from the way it is called. Sometimes this function
declaration is correct, but when it isn’t, it can be a difficult bug to
find.
Because all functions
must
be declared before they are used in C++,
the opportunity for this problem to pop up is greatly diminished.
The C++ compiler refuses to declare a function automatically for
f(char)
. Thus, the
compilation is successful. In C, the linker would also be successful,
but
not
in C++. Because the compiler decorates the names, the
definition becomes something like
f_int
, whereas the use of the
function is
f_char
. When the linker tries to resolve the reference to
f_char
, it can only find
f_int
, and it gives you an error message.
This is type-safe linkage. Although the problem doesn’t occur all
that often, when it does it can be incredibly difficult to find,
especially in a large project. This is one of the cases where you can
easily find a difficult error in a C program simply by running it
through the C++ compiler.
7: Function Overloading & Default Arguments 333
Overloading example
We can now modify earlier examples to use function overloading.
As stated before, an immediately useful place for overloading is in
constructors. You can see this in the following version of the
Stash
class:
//: C07:Stash3.h
is set to zero, along with the
storage
pointer. In the second constructor, the call to
inflate(initQuantity)
increases
quantity
to the allocated size:
//: C07:Stash3.cpp {O}
// Function overloading
#include "Stash3.h"
#include " /require.h"
#include <iostream>
#include <cassert>
334 Thinking in C++ www.BruceEckel.com
using namespace std;
const int increment = 100;
Stash::Stash(int sz) {
size = sz;
quantity = 0;
next = 0;
storage = 0;
}
Stash::Stash(int sz, int initQuantity) {
size = sz;
quantity = 0;
next = 0;
}
7: Function Overloading & Default Arguments 335
int Stash::count() {
return next; // Number of elements in CStash
}
void Stash::inflate(int increase) {
assert(increase >= 0);
if(increase == 0) return;
int newQuantity = quantity + increase;
int newBytes = newQuantity * size;
int oldBytes = quantity * size;
unsigned char* b = new unsigned char[newBytes];
for(int i = 0; i < oldBytes; i++)
b[i] = storage[i]; // Copy old to new
delete [](storage); // Release old storage
storage = b; // Point to new memory
quantity = newQuantity; // Adjust the size
} ///:~
When you use the first constructor no memory is allocated for
storage
. The allocation happens the first time you try to
add( )
an
object and any time the current block of memory is exceeded inside
add( )
.
Both constructors are exercised in the test program:
<< cp << endl;
} ///:~
The constructor call for
stringStash
uses a second argument;
presumably you know something special about the specific
problem you’re solving that allows you to choose an initial size for
the
Stash
.
unions
As you’ve seen, the only difference between
struct
and
class
in C++
is that
struct
defaults to
public
and
class
defaults to
private
. A
struct
can also have constructors and destructors, as you might
expect. But it turns out that a
union
float U::read_float() { return f; }
int main() {
U X(12), Y(1.9F);
cout << X.read_int() << endl;
cout << Y.read_float() << endl;
} ///:~
You might think from the code above that the only difference
between a
union
and a
class
is the way the data is stored (that is,
the
int
and
float
are overlaid on the same piece of storage).
However, a
union
cannot be used as a base class during
inheritance, which is quite limiting from an object-oriented design
standpoint (you’ll learn about inheritance in Chapter 14).
Although the member functions civilize access to the
union
somewhat, there is still no way to prevent the client programmer
from selecting the wrong element type once the
union
SuperVar(int ii);
SuperVar(float ff);
void print();
};
SuperVar::SuperVar(char ch) {
vartype = character;
c = ch;
}
SuperVar::SuperVar(int ii) {
vartype = integer;
i = ii;
}
SuperVar::SuperVar(float ff) {
vartype = floating_point;
f = ff;
}
void SuperVar::print() {
switch (vartype) {
case character:
cout << "character: " << c << endl;
break;
case integer:
cout << "integer: " << i << endl;
break;
case floating_point:
cout << "float: " << f << endl;
but doesn’t
require accessing the
union
elements with a variable name and the
dot operator. For instance, if your anonymous
union
is:
//: C07:AnonymousUnion.cpp
int main() {
union {
int i;
float f;
};
// Access members without using qualifiers:
i = 12;
f = 1.22;
} ///:~
Note that you access members of an anonymous union just as if
they were ordinary variables. The only difference is that both
variables occupy the same space. If the anonymous
union
is at file
scope (outside all functions and classes) then it must be declared
static
so it has internal linkage.
Although
SuperVar
is now safe, its usefulness is a bit dubious
because the reason for using a
. They don’t
seem all that different, do they? In fact, the first constructor seems
to be a special case of the second one with the initial
size
set to
zero. It’s a bit of a waste of effort to create and maintain two
different versions of a similar function.
C++ provides a remedy with
default arguments
. A default argument
is a value given in the declaration that the compiler automatically
inserts if you don’t provide a value in the function call. In the
Stash
example, we can replace the two functions:
Stash(int size); // Zero quantity
Stash(int size, int initQuantity);
with the single function:
Stash(int size, int initQuantity = 0);
The
Stash(int)
definition is simply removed – all that is necessary is
the single
Stash(int, int)
definition.
Now, the two object definitions
Stash A(100), B(100, 0);
Default arguments are only placed in the declaration of a function
(typically placed in a header file). The compiler must see the
default value before it can use it. Sometimes people will place the
commented values of the default arguments in the function
definition, for documentation purposes
void fn(int x /* = 0 */) { //
342 Thinking in C++ www.BruceEckel.com
Placeholder arguments
Arguments in a function declaration can be declared without
identifiers. When these are used with default arguments, it can look
a bit funny. You can end up with
void f(int x, int = 0, float = 1.1);
In C++ you don’t need identifiers in the function definition, either:
void f(int x, int, float flt) { /* */ }
In the function body,
x
and
flt
can be referenced, but not the
middle argument, because it has no name. Function calls must still
provide a value for the placeholder, though:
f(1)
or
f(1,2,3.0)
. This
syntax allows you to put the argument in as a placeholder without
using it. The idea is that you might want to change the function
Mem(int sz);
~Mem();
int msize();
byte* pointer();
byte* pointer(int minSize);
};
#endif // MEM_H ///:~
A
Mem
object holds a block of
byte
s and makes sure that you have
enough storage. The default constructor doesn’t allocate any
storage, and the second constructor ensures that there is
sz
storage
in the
Mem
object. The destructor releases the storage,
msize( )
tells
you how many bytes there are currently in the
Mem
object, and
pointer( )
produces a pointer to the starting address of the storage
(
Mem
is a fairly low-level tool). There’s an overloaded version of
Mem::~Mem() { delete []mem; }
int Mem::msize() { return size; }
void Mem::ensureMinSize(int minSize) {
if(size < minSize) {
byte* newmem = new byte[minSize];
memset(newmem + size, 0, minSize - size);
memcpy(newmem, mem, size);
delete []mem;
mem = newmem;
size = minSize;
}
}
byte* Mem::pointer() { return mem; }
byte* Mem::pointer(int minSize) {
ensureMinSize(minSize);
return mem;
} ///:~
You can see that
ensureMinSize( )
is the only function responsible
for allocating memory, and that it is used from the second
constructor and the second overloaded form of
pointer( )
. Inside
// Testing the Mem class
//{L} Mem
#include "Mem.h"
#include <cstring>
#include <iostream>
using namespace std;
class MyString {
Mem* buf;
public:
MyString();
MyString(char* str);
~MyString();
void concat(char* str);
void print(ostream& os);
};
MyString::MyString() { buf = 0; }
MyString::MyString(char* str) {
buf = new Mem(strlen(str) + 1);
strcpy((char*)buf->pointer(), str);
}
void MyString::concat(char* str) {
if(!buf) buf = new Mem;
strcat((char*)buf->pointer(
buf->msize() + strlen(str) + 1), str);
}
constructor is that you can create, for example, a large array of
empty
MyString
objects very cheaply, since the size of each object
is only one pointer and the only overhead of the default constructor
is that of assigning to zero. The cost of a
MyString
only begins to
accrue when you concatenate data; at that point the
Mem
object is
created if it hasn’t been already. However, if you use the default
constructor and never concatenate any data, the destructor call is
still safe because calling
delete
for zero is defined such that it does
not try to release storage or otherwise cause problems.
If you look at these two constructors it might at first seem like this
is a prime candidate for default arguments. However, if you drop
the default constructor and write the remaining constructor with a
default argument:
MyString(char* str = "");
7: Function Overloading & Default Arguments 347
everything will work correctly, but you’ll lose the previous
efficiency benefit since a
Mem
object will always be created. To get
the efficiency back, you must modify the constructor:
MyString::MyString(char* str) {
class. If you look at the
definitions of the two constructors and the two
pointer( )
functions,
you can see that using default arguments in both cases will not
cause the member function definitions to change at all. Thus, the
class could easily be:
//: C07:Mem2.h
#ifndef MEM2_H
#define MEM2_H
348 Thinking in C++ www.BruceEckel.com
typedef unsigned char byte;
class Mem {
byte* mem;
int size;
void ensureMinSize(int minSize);
public:
Mem(int sz = 0);
~Mem();
int msize();
byte* pointer(int minSize = 0);
};
#endif // MEM2_H ///:~
Notice that a call to
ensureMinSize(0)
will always be quite
efficient.
Although in both of these cases I based some of the decision-
read them, especially if the class creator can order the arguments so
the least-modified defaults appear latest in the list.
An especially important use of default arguments is when you start
out with a function with a set of arguments, and after it’s been used
for a while you discover you need to add arguments. By defaulting
all the new arguments, you ensure that all client code using the
previous interface is not disturbed.
Exercises
Solutions to selected exercises can be found in the electronic document
The Thinking in C++ Annotated
Solution Guide
, available for a small fee from www.BruceEckel.com.
1. Create a
Text
class that contains a
string
object to hold
the text of a file. Give it two constructors: a default
constructor and a constructor that takes a
string
argument that is the name of the file to open. When the
second constructor is used, open the file and read the
contents into the
string
member object. Add a member
function
contents( )
to return the
argument, which
it prints in addition to the internal message. Does it make
350 Thinking in C++ www.BruceEckel.com
sense to use this approach instead of the one used for the
constructor?
3. Determine how to generate assembly output with your
compiler, and run experiments to deduce the name-
decoration scheme.
4. Create a class that contains four member functions, with
0, 1, 2, and 3
int
arguments, respectively. Create a
main( )
that makes an object of your class and calls each of the
member functions. Now modify the class so it has
instead a single member function with all the arguments
defaulted. Does this change your
main( )
?
5. Create a function with two arguments and call it from
main( )
. Now make one of the arguments a “placeholder”
(no identifier) and see if your call in
main( )
changes.
6. Modify
Stash3.h
and
Stash3.cpp
enumeration (with no
instance) and modify
print( )
so that it requires a
vartype
argument to tell it what to do.
9. Implement
Mem2.h
and make sure that the modified
class still works with
MemTest.cpp
.
10. Use
class Mem
to implement
Stash
. Note that because
the implementation is
private
and thus hidden from the
client programmer, the test code does not need to be
modified.
7: Function Overloading & Default Arguments 351
11. In
class Mem
, add a
bool
moved( )
constant
(expressed by the const
keyword) was created to allow the programmer to
draw a line between what changes and what doesn’t.
This provides safety and control in a C++
programming project.
354 Thinking in C++ www.BruceEckel.com
Since its origin,
const
has taken on a number of different purposes.
In the meantime it trickled back into the C language where its
meaning was changed. All this can seem a bit confusing at first, and
in this chapter you’ll learn when, why, and how to use the
const
keyword. At the end there’s a discussion of
volatile
, which is a near
cousin to
const
(because they both concern change) and has
identical syntax.
The first motivation for
const
seems to have been to eliminate the
use of preprocessor
#define
s for value substitution. It has since
been put to use for pointers, function arguments, return types, class
objects and member functions. All of these have slightly different