-3

I am curious what exactly you pass in when using a class object as a parameter with array brackets after. The format i am talking about is

class Class {};

//This is just an example
void Function( int x, int y, Class object[] ) {
    double stuff = object[x].getThing();
    double stuffed = object[y].getThing();
}
mychsmit1
  • 11
  • 1
  • Is that actually the code or do you write `class` where you actually mean an identifier that is a class name. The actual code you have, the argument can not be used. – M.M Jan 20 '20 at 01:58

1 Answers1

1

A function parameter having an array type is implicitly adjusted by the compiler to pointer to the array element type.

So for example these function declarations

void f( T a[] );
void f( T a[1] );
void f( T a[100] );

where T is some arbitrary type as for example

class Class
{
    //…
};

declare the same one function that is equivalent to the declaration

void f( T *a );

You may include all these declarations in one compilation unit though the compiler can issue a warning that these declarations are redundant.

On the other hand, an array passed to such a function is in turn implicitly converted to pointer to its first element.

Of course you can even pass to such a function a pointer to a single object instead of specifying an array as an argument because in any case the function deals with a pointer.

Here is a demonstrative program.

#include <iostream>

class Class
{
public:
    int x = 10;
};

void f( Class object[] );

//  The definition of the above function
void f( Class *object )
{
    std::cout << object[0].x << '\n';
}

int main() 
{
    Class object;
    Class objects[10];

    f( &object );
    f( objects );

    return 0;
}

Its output is

10
10
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335