| |
 | Defining Functions with a Variable Argument List
By Danny Kalev, C++ Pro
|
A function usually takes an immutable number of arguments whose types are fixed when its code is compiled. In certain applications, however, you need to pass to the same function a variable number of arguments. Consider a debugging function that displays variables to the screen or writes them to a file. Restricting the function to a fixed number of arguments of predetermined types makes it virtually uselessat one breakpoint, you want to examine a single variable whereas at another you may need to examine an entire array.
Fortunately, C provides a special mechanism for defining functions that take a variable number of arguments. These are known as variable argument list functions or variadic functions. printf() and scanf() are classic examples of variadic functions. Although C++ offers superior solutions to this problem, familiarity with variadic functions is still advantageous as most C++ implementations for embedded systems don't support templates or wide-character streams. Furthermore, in pure C environments, variadic functions have no alternative. In the following sections I will first explain how to define and use a variadic function. Next, I will examine object-oriented methods for dealing with a variable list of arguments and list their pros and cons.

Passing a variable number of arguments to the same function on each invocation can be useful under some circumstances. However, C++ doesn't allow you to change the number arguments that a function takes.

Use a variadic function.
|
|
Find Out More
The Embedded C++ HOMEPAGE
MSDN Online Library: Calling Conventions Topics
MSDN Online Library: __fastcall
 | TALK BACK |
This is a classic example of C++ as a multi-paradigm programming language:
you can either use pure C-style procedural programming or a
state-of-the-art object-oriented approach to achieve the same goal. Do
you consider this versatility a boon or bust? In other words, would you
prefer to see C++ getting rid of its backward compatibility with C and
thus becoming a pure object-oriented language, or would you prefer to keep C++ as a
superset of C? Share your opinions in the C++ discussion group.
 |
|