Re: Declare an array parameter to be const?
From: Andrey Tarasevich (andreytarasevich_at_hotmail.com)
Date: 01/27/05
- Next message: Matthias: "Re: Local class instances invalid template argument"
- Previous message: Matthias: "Re: Is this (tiny) function portable?"
- In reply to: Dylan Nicholson: "Declare an array parameter to be const?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Thu, 27 Jan 2005 10:30:55 -0800
Dylan Nicholson wrote:
> Is there any way of declaring the parameter "array" below to be const,
> so that the function as written will not compile:
>
> void foo(int array[] /*const*/)
> {
> array = array; // should not be allowed to point this at another
> array!
> }
As long as you are using this syntax, it can't be done in C++.
Note, that what you declare in this case is not an array but a mere
pointer. Which means that you can switch to the alternative (and
absolutely equivalent) way to declare the same parameter
void foo(int* array);
and then add const qualifications as you please
void foo(const int* array);
void foo(int* const array); // <- this is the one you need
void foo(const int* const array);
As a side note, in the C99 C language you can archive what you need with
your original syntax
void foo(int array[const]);
but this is ill-formed in C++.
-- Best regards, Andrey Tarasevich
- Next message: Matthias: "Re: Local class instances invalid template argument"
- Previous message: Matthias: "Re: Is this (tiny) function portable?"
- In reply to: Dylan Nicholson: "Declare an array parameter to be const?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|