const is very much preferred in C++, primarily because it is type-safe. The pre-processor is just an 'on the fly' editor of your code, doing some fairly crude search/replace text substitutions. Occasionally, this produces some real surprises for the programmer.
In C, you have to use the pre-processor for things like
Code:
#define SIZE 10
#define max(a,b) a>b?a:b /* this is a dangerous macro */
int arr[SIZE];
Because you can't do
Code:
const int SIZE = 10;
int arr[SIZE]; /* doesn't work in C */
In C++, you would do this
Code:
const int SIZE = 10;
inline int max( int a, int b ) {
return a>b?a:b;
}
int arr[SIZE];
Inline functions are as fast as the macro versions, but again they are type-safe.
For example, this would compile OK using the #define, but would produce some odd results at run-time.
It would not compile at all with the C++ inline function
Code:
char *p = max( "hello", "world" );
In addition, the inline function does not suffer from multiple evaluation problems, like
Code:
int c = max( a++, b++ );
In C (using the macro), this expands to
Code:
int c = a++ > b++ ? a++ : b++;
Which results in either a or b being incremented TWICE (almost certainly not what you wanted / expected).