C++ Tips from Thinking in C++(1)

The following C++ grammar items are extracted from the book Thinking in C++. Most of them are often forgotten by myself. I will record here for my future quick reference.

Please note that the page info is for the English version of this book.
1. If a variable is defined as static variable, then it cannot be referenced by other files by using “extern”. p149~p150.

2. Anonymous union. p319The union has no type name and no variable name.For instance, if your anonymous union is:

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.

3. Some rules of default argument. p321~p322
There are two rules you must be aware of when using default arguments. First, only trailing arguments may be defaulted. That is, you can’t have a default argument followed by a non-default
argument. Second, once you start using default arguments in a
particular function call, all the subsequent arguments in that
function’s argument list must be defaulted (this follows from the
first rule).

4. const in head files. p334~p338
To use const instead of #define, you must be able to place const
definitions inside header files as you can with #define. This way,
you can place the definition for a const in a single place and
distribute it to translation units by including the header file. A
const in C++ defaults to internal linkage; that is, it is visible only
within the file where it is defined and cannot be seen at link time by
other translation units. If you make an explicit declaration using extern with const:

extern const int bufsize;

however, you force storage to be allocated (this
is also true for certain other cases, such as taking the address of a
const). Storage must be allocated because extern says “use external
linkage,” which means that several translation units must be able to
refer to the item, which requires it to have storage. In the ordinary case, when extern is not part of the definition, no
storage is allocated. When the const is used, it is simply folded in at
compile time. This situation is very different in C.
In C, a const always occupies storage and its name is global. The C
compiler cannot treat a const as a compile-time constant. In C, if you say

const int bufsize = 100;
char buf[bufsize];

you will get an error, even though it seems like a rational thing to
do. Because bufsize occupies storage somewhere, the C compiler
cannot know the value at compile time. You can optionally say

const int bufsize;

in C, but not in C++, and the C compiler accepts it as a declaration
indicating there is storage allocated elsewhere. Because C defaults
to external linkage for consts, this makes sense. C++ defaults to
internal linkage for consts so if you want to accomplish the same
thing in C++, you must explicitly change the linkage to external
using extern:

extern const int bufsize; // Declaration only

This line also works in C.

Leave a Reply

Your email address will not be published. Required fields are marked *