I need to implement an array of float of size N, but for which the first element always needs to be 1.0.
1 - I could write a code, making sure I do not ever modify array[0]. But I was hopping of defining this first value using the const key word, so the compiler would warned me, if I was trying to edit this element.
2 - I could also create a Class for such object, but I was wondering if there is a simpler way.
I don't think you're expected to write a class for this. It's more likely that you're expected to write a single line of code that defines and initialises the array.
The c-style array (and C++ sequence containers) hold sequences of elements which are all of the same static type.
In an array T[N], all the elements of the array are of type T.
If T is float, then all elements are of type float;
if T is const float, then all elements are of type const float.
> I could also create a Class for such object, but I was wondering if there is a simpler way.
There is no simpler way;
either a class for the simulated array, or a class (union-like-class) for the elements of the array.
you can make a class and then make an array of that class to do it, but this is serious bloatware. The class would overload the assignment operator and have a member for "read only". if read-only, the assignment operator would do nothing or fail loudly. Only the first element would have this flag set.
This however isnt an array of float; its an array of your class, so that violates the design already, even though your class can be made to look and behave exactly like a float and even be interchanged with one everywhere, its still technically not a float.
you can also use a pointer to hide the first element.
access can't get to the first location with positive index, so you can use it wherever you write to the array, but the index is off by one from the real array.
Personally I would go with option 3 which is:
do not store the 1.0. Whatever purpose it serves can be replaces with an array of float + a const that = 1.0 in your logic. You can tie the 2 together in a C struct:
The "array" is for some purpose. Does it really have to be a flock with one black sheep in it? Will it really be used in a way that allows only "arrayish" objects?
Question the design. When a design demands something overly complex and unorthodox, then the real problem might not be in the implementation.
This array is supposed to be the input layer or hidden layer of an artificial neural network. Mathematically, when you have N inputs, it is convenient to consider N+1 inputs with the first one equal to 1.
I won't go into details but this is the short version.
if you are doing a sum of products over it, I recommend option 4, just set it to 1 and don't change that location. Oddball logic would just slow your SOP code down and the last thing ANN code needs is slowness.