Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does this Method declaration/definition mean? (something to do with passing an array?)

Hi I was stumbling through legacy code, and I came across a wierd method definition/declaration. I have an educated guess of what it does, but I cannot be 100% sure yet.

declaration:

const SomeEnumeratedId (&SomeMethod() const)[SOME_CONSTANT_VALUE];

definition

const SomeEnumeratedId (&SomeClass::SomeMethod() const)[SOME_CONSTANT_VALUE]
{
    return someMemberArray;
}

My best guess is that it is passing a reference to someMemberArray and that it is guaranteeing that it is of size SOME_CONSTANT_VALUE, but I have never seen the [] notation after the method declaration as it appears, and there are so many parentheses.

Any help greatly appreciated.

like image 301
Michael Avatar asked Jan 14 '11 16:01

Michael


People also ask

What does it mean to declare an array?

An "array declaration" names the array and specifies the type of its elements. It can also define the number of elements in the array. A variable with array type is considered a pointer to the type of the array elements.

What happens when you pass an array to a method Java?

You can pass arrays to a method just like normal variables. When we pass an array to a method as an argument, actually the address of the array in the memory is passed (reference). Therefore, any changes to this array in the method will affect the array.

What is declaration in array in Java?

Arrays are used to store multiple values in a single variable, instead of declaring separate variables for each value. To declare an array, define the variable type with square brackets: String[] cars; We have now declared a variable that holds an array of strings.

What is declaration of array and example?

When a function parameter is declared as an array, the compiler treats the declaration as a pointer to the first element of the array. For example, if x is a parameter and is intended to represent an array of integers, it can be declared as any one of the following declarations: int x[]; int *x; int x[10];


1 Answers

It's the declaration of a const member function taking no parameters and returning a reference to an array of SOME_CONSTANT_VALUE const SomeEnumeratedIds.

It looks easier to understand with a typedef.

typedef const SomeEnumeratedId SomeArrayType[SOME_CONSTANT_VALUE];

SomeArrayType& SomeClass::SomeMethod() const
{
    return someMemberArray;
}
like image 50
CB Bailey Avatar answered Nov 15 '22 04:11

CB Bailey