Escolar Documentos
Profissional Documentos
Cultura Documentos
PROGRAMMING POINTERS
Dan Saks
Top-Level cv-Qualifiers
in Function Parameters
For the past few months, Ive been void f(T volatile *); g(k);
discussing the role of cv-qualifiers (the
keywords const and volatile) appear- and this assortment of pointer copies argument k to parameter n
ing in function parameter declara- variables: without altering k. Even if you
tions. Ive been focusing on how you declared k as:
can overload functions whose parame- T *p;
ter types differ only in their use of cv- T const *pc; int const k = 1024;
qualifiers,1 and why you might want to
do so.2,3
Although cv-qualifiers that appear Although the const and volatile qualifiers usually
in a parameter declaration usually
become part of the functions signa- add valuable compile-time type information to
ture, theyre not always part of the
signature. There are times when C++ your programs, they dont always live up to all of
ignores cv-qualifiers in parameter
declarations as it determines a func- your expectations.
tions signature. In contrast, C never
ignores cv-qualifiers in parameter T volatile *pv;
declarations. This subtle difference the call g(k) would work just as well.
between C++ and C is my topic for the expression f(p) calls f(T *), f(pc) You can pass a constant argument as a
this month. calls f(T const *), and f(pv) calls nonconstant parameter, again because
f(T volatile *). passing by value just makes a copy of
Passing by value vs. by address The previous example illustrates the argument. In this example, para-
overloading with cv-qualifiers using meter n is nonconstant, so g can
When a C++ compiler encounters a functions that pass parameters by change ns value. However, changing
call to an overloaded function, it address. Lets see how the behavior ns value inside g has no effect on ks
selects the function to be called by changes when functions pass para- value because n doesnt refer back to k
matching the types of the actual argu- meters by value rather than by in any way.
ments in the call against the types of address. Changing gs declaration to:
the formal parameters in one of the For example, a function g declared
function declarations. The compilers as: int g(int const n);
overload resolution process often
takes cv-qualifiers into account. For int g(int n); has no effect on code that calls g. A
example, given three overloaded call such as g(k) still copies argument
functions: has a parameter n passed by value. A k to parameter n without altering k. It
call to g as in: doesnt matter whether k is constant.
void f(T *); You can always read the value of a con-
void f(T const *); int k; stant; you just cant write to it.
... Although declaring parameter n as
On the other hand, in: and remains an active member. With Parameter Types, Embedded Systems
Thomas Plum, he wrote C++ Programming, September 1999, p. 77.
int f(char *const p); Programming Guidelines (Plum- 2. Saks, Dan, Overloading with const,
Hall). You can write to him at Embedded Systems Programming,
the const qualifier is at the top level, so dsaks@wittenberg.edu. December 1999, p. 81.
it is not part of the functions signa- 3. Saks, Dan, More on Overloading with
ture. This function has the same sig- References const, Embedded Systems
nature as: 1. Saks, Dan, Using const and volatile in Programming, January 2000, p. 71.
More to come